Req #51016 [Com]: Always associate "else:" followed by colon to an "if:" followed by colon
Edit report at https://bugs.php.net/bug.php?id=51016&edit=1
ID: 51016
Comment by: temp at springtimesoftware dot com
Reported by: claude dot pache at gmail dot com
Summary: Always associate "else:" followed by colon to an
"if:" followed by colon
Status: Open
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: *
PHP Version: 5.3.1
Block user comment: N
Private report: N
New Comment:
1. PHP should work like most other languages: an else should always be associated with the nearest
previous if statement. Period. Any other behavior can be obtained by using curly brackets.
2. Currently, the PHP Manual is incomplete in describing valid sequences of nested ifs, thens, and
elses. It should be improved to specify what happens e.g. in the following dangling else case:
if ($a)
if ($b) b();
else c();
Of course, the "else c();" should be associated with "if ($b) b();".
3. Colons following if, then, or else tokens should result in a syntax error.
4. "elseif" or "elif" (your choice) should be a valid abbreviation for
"else if".
Previous Comments:
------------------------------------------------------------------------
[2010-02-11 14:03:44] claude dot pache at gmail dot com
Description:
------------
In case of ambiguity, the statements "else" and "elseif" are always
associated to the last previous "if" or "elseif". This rule is not
convenable when the else/elseif is followed by a colon and the last
previous if/elseif is not.
See also: http://bugs.php.net/bug.php?id=838
Reproduce code:
---------------
if(foo):
// some code here
if (bar) baz;
else:
// some code here
endif;
Expected result:
----------------
Since the "else" is followed by a colon (:), it can be associated
*unambiguously* with the last "if" followed by a colon (here,
"if(foo):"), and not with the last "if" (here, "if(bar)").
Actual result:
--------------
Parse error. The "else" is associated with the last "if", and the
following ":" is unexpected.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=51016&edit=1
Thread (3 messages)