php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51016 Always associate "else:" followed by colon to an "if:" followed by colon
Submitted: 2010-02-11 14:03 UTC Modified: 2010-11-24 09:41 UTC
Votes:4
Avg. Score:3.2 ± 1.5
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: claude dot pache at gmail dot com Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.3.1 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
46 - 4 = ?
Subscribe to this entry?

 
 [2010-02-11 14:03 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-24 09:41 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: irrelevant +Operating System: *
 [2021-04-26 12:19 UTC] temp at springtimesoftware dot com
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".
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Sep 18 06:03:37 2021 UTC