Bug #75138 [Opn->Nab]: Nested "IF" triggers Exception when it is before "else:" or "elseif:".

From: Date: Wed, 30 Aug 2017 15:57:35 +0000
Subject: Bug #75138 [Opn->Nab]: Nested "IF" triggers Exception when it is before "else:" or "elseif:".
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210881@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75138&edit=1 ID: 75138 Updated by: peehaa@php.net Reported by: jcmarchi at gmail dot com Summary: Nested "IF" triggers Exception when it is before "else:" or "elseif:". -Status: Open +Status: Not a bug Type: Bug Package: *Programming Data Structures Operating System: All PHP Version: Irrelevant Block user comment: N Private report: N New Comment: From the docs (http://php.net/manual/en/control-structures.alternative-syntax.php): > Mixing syntaxes in the same control block is not supported. Previous Comments: ------------------------------------------------------------------------ [2017-08-30 15:31:47] jcmarchi at gmail dot com Description: ------------ When using Block Alternative Syntax for IF statement (if: elseif: else: endif;) a nested "IF" will trigger a PHP EXCEPTION error saying: syntax error, unexpected ':' if it exists immediately BEFORE the "else:" or "elseif". Test script: --------------- Example 1: <?php $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; ?> Example 2: <?php $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; ?> Both examples fail with the same error. Expected result: ---------------- Test Scripts fail. So, this is not a result but a "workaround" for the interpreter to stop failing. It fixes the parsing error to add an extra semicolon AFTER the last semicolon (before the "elseif:" or "else:") as shown below: <?php $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; ?> Or, for the Example 2 (nested traditional "if" with brackets), to add a semicolon AFTER the last closing bracket: <?php $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; ?> Ultimately, an empty line with a semicolon in it also do the trick, but none are "PHP standard" and create a huge trouble when debugging. Some people may even get mistaken with the statements and do something foolish like this broken logic (which is validated by the PHP interpreter, of course): <?php $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; ?> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75138&edit=1

« previous php.bugs (#210881) next »