-1

I am running the PHPStorm debugger with php 7.1.30 and when I run this code:

    if (condition_1){
      do something;
    }
    elseif (condition_2) {
      do something different;
    }
    elseif (condition_3) {
      do something more different;
    }
    else {
      do something if all else has failed;
    }

if condition_1 is false, control immediately passes to the else case, and the elseif tests are not applied.

If I replace the elseif with else if, everything works as expected.

1 Answers1

1

https://www.php.net/manual/en/control-structures.elseif.php

They are the same, the only reason why it would not work would be if you were using colons, as detailed in the manual :

Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.

    <?php

/* Incorrect Method: */
if ($a > $b):
    echo $a." is greater than ".$b;
else if ($a == $b): // Will not compile.
    echo "The above line causes a parse error.";
endif;


/* Correct Method: */
if ($a > $b):
    echo $a." is greater than ".$b;
elseif ($a == $b): // Note the combination of the words.
    echo $a." equals ".$b;
else:
    echo $a." is neither greater than or equal to ".$b;
endif;

?>

But since you are using curly brackets, should work with either elseif or else if.

Jay
  • 26
  • 4
  • Thanks for these comments. Apparently I didn't make myself clear. (1) I am using curly brackets, so the colon problem would not arise. (2) The behavior I am observing is NOT what the manual describes. When I use the "if ... elseif" construct, the elseif statements are skipped when the if statement evaluates to false. When I use the "if ... else if" construct the else if statements are evaluated. – Wyckham Seelig Apr 30 '20 at 15:15