Consider the following hypothetical PHP example:
$bar = 'bar';
$foo = 'foo';
if (isset($bar)):
if (isset($foo)) echo "Both are set.";
elseif (isset($foo)):
echo "Only 'foo' is set.";
else:
echo "Only 'bar' is set.";
endif;
Disconsider the dumb logic and focus on the elseif
line. If you try it yourself you will get a PHP EXCEPTION error saying "syntax error, unexpected ':' "
Now, you may think the fix is to have the sub-if
enclosed in between { }
instead of being a single line statement, like this:
$foo = 'foo';
$bar = 'bar';
if (isset($bar)):
if (isset($foo)) {
echo "Both are set.";
}
elseif (isset($foo)):
echo "Only 'foo' is set.";
else:
echo "Only 'bar' is set.";
endif;
Wrong! The error remains. Exactly the same EXCEPTION as before...
So, what is wrong with those examples?
Well, here is what I found: if you put a semicolon (
;
) AFTER the curly bracket (}
) which resides immediately before theelseif
statement, then the error is gone! Try it:Weird enough, if you go back to the first example and DOUBLE the semicolon immediately before the
elseif
statement, it will also work:But, it doesn't end there. You can also do this:
However, in this last example, the logic gets totally scrambled! The
elseif
will now belong to the sub-if
instead of the firstif
, and the rest of the logic will all behave as a "one single statement" in response to the firstif
only. Very confusing and error prone (be careful).The differences are very subtle and can deceive the eyes (especially while debugging). For this reason, I strongly suggest the first example from this answer: when using IF-ELSEIF blocks (AKA "Alternative Syntax"), if another IF is required inside it, enclose it in between
{}
and don't forget to add a semicolon after the last}
. Example:Maybe the truth is that someone screwed up in the language parsing process for those PHP Block Alternative Statements or failed to document this very important detail!