Search code examples
phparraysdrupaldrupal-7

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!


Solution

  • 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    <------ !!!!