mardi 29 août 2017

Is there a BUG in PHP "IF block" parsing method or what?

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?

Aucun commentaire:

Enregistrer un commentaire