jasonchen
jasonchen

Reputation: 3

comparing array with int in PHP

I'm reading through the drupal_bootstrap code at: https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_bootstrap/7 to try to understand how to bootstrap drupal code. I have experience in other languages but not php. This line of code below puzzles me, because $phases is an array, and everything else is int. What does it mean when it compares array with an int?

while ($phases && $phase > $stored_phase && $final_phase > $stored_phase) {
      $current_phase = array_shift($phases);

Thanks!

Upvotes: 0

Views: 56

Answers (2)

fusion3k
fusion3k

Reputation: 11689

In php basic comparison, an array with elements==True and an empty array==False.
array_shift() reduce the size of array.

So, in your example, the loop reduce the size of $phases until the $phases is empty.
(better: until $phases is empty or one of the other conditions are False)

Edit:

There is not a comparison between array and integer, the condition is:
ARRAY IS NOT EMPTY AND INT > INT AND INT > INT.

Edit 2:

Please note that there are a sort of incongruity in php type juggling comparison:

array() == False
''      == False
0       == False

but:

''      == 0
array() != ''   <------ !!!!
array() != 0    <------ !!!!

Upvotes: 2

2pha
2pha

Reputation: 10165

it does not compare array to int.
the first part of the condition where it uses $phases just checks that it has a value.

Definition of bootstrap in Computing:

a technique of loading a program into a computer by means of a few initial instructions which enable the introduction of the rest of the program from an input device.

Upvotes: 1

Related Questions