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: 2021-11-16 15:17 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: cmb (profile)
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.3.1 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: claude dot pache at gmail dot com
New email:
PHP Version: OS:

 

 [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".
 [2021-11-16 15:17 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2021-11-16 15:17 UTC] cmb@php.net
> This rule is not convenable when the else/elseif is followed by
> a colon and the last previous if/elseif is not.

Right.  However, I wouldn't want to encourage anyone to use this
mixed style.  Given that this request is more than ten years old
and got almost no traction, I'm closing as WONTFIX.  If you, or
anybody else for that matter, is still interested in this, please
pursue the RFC process[1].

> It should be improved to specify what happens e.g. in the
> following dangling else case:

Done[2].

[1] <https://wiki.php.net/rfc/howto>
[2] <https://github.com/php/doc-en/commit/3725fe1c95b201563518c30e1988b2f32ee099aa>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC