|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-05-13 04:05 UTC] faisun at sina dot com
Description:
------------
PHP Version: 5.2.9.2
<?
if(2>1):
echo "ABCD";
if(4>3){ echo "EFGH"; }
else:
echo "123456";
endif;
?>
Result: Parse error: parse error in E:\wwwroot\1.php on line 5
<?
if(2>1):
echo "ABCD";
if(4>3){ echo "EFGH"; } else{}
else:
echo "123456";
endif;
?>
Result:ABCDEFGH
Reproduce code:
---------------
<?
if(2>1):
echo "ABCD";
if(4>3){ echo "EFGH"; }
else:
echo "123456";
endif;
?>
Expected result:
----------------
ABCDEFGH
Actual result:
--------------
Parse error: parse error in E:\wwwroot\1.php on line 5
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 20:00:01 2025 UTC |
So, what's happening is that the parser is associating the else: with the normal style if block on the line immediately above it, and of course mixing in a single if/else like that is verboten. Should it be fixed? Probably, but it's a serious edge case, and massaging the parser to cope with it is much harder than the following workaround: <? if(2>1): echo "ABCD"; if(4>3){ echo "EFGH"; }; else: echo "123456"; endif; ?> Note the extra semicolon added after the inner-if statement. This is enough to terminate that expression and ensure that the else statement binds to the right if. Leaving this bug report open, because it is definitely a bug, but I'm not sure who's going to invest the cycles on it.