Bug #75138 [NEW]: Nested "IF" triggers Exception when it is before "else:" or "elseif:".

From: Date: Wed, 30 Aug 2017 15:31:52 +0000
Subject: Bug #75138 [NEW]: Nested "IF" triggers Exception when it is before "else:" or "elseif:".
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-210880@lists.php.net to get a copy of this message
From: jcmarchi at gmail dot com Operating system: All PHP version: Irrelevant Package: *Programming Data Structures Bug Type: Bug Bug description:Nested "IF" triggers Exception when it is before "else:" or "elseif:". 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 bug report at https://bugs.php.net/bug.php?id=75138&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75138&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75138&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75138&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=75138&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=75138&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=75138&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=75138&r=needscript Try newer version: https://bugs.php.net/fix.php?id=75138&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=75138&r=support Expected behavior: https://bugs.php.net/fix.php?id=75138&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=75138&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=75138&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=75138&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75138&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=75138&r=dst IIS Stability: https://bugs.php.net/fix.php?id=75138&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=75138&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=75138&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=75138&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=75138&r=mysqlcfg

« previous php.bugs (#210880) next »