php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48255 Mixing control structure syntaxes causes parse error
Submitted: 2009-05-13 04:05 UTC Modified: 2016-01-15 13:43 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: faisun at sina dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.*, 6CVS (2009-05-12) 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: faisun at sina dot com
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-05 21:16 UTC] whatrevolution at yahoo dot com
Bug OP test, result:

( ! ) Parse error: syntax error, unexpected ':' in /var/www/php_bugs/mixed_control_structure.php on line 5


PHP Version 5.2.10-2ubuntu6.4

System 	Linux 2.6.31-20-generic x86_64
Build Date 	Jan 6 2010 22:36:47
Server API 	Apache 2.0 Handler 
PHP API 	20041225
PHP Extension 	20060613
Zend Extension 	220060519
Debug Build 	no
Thread Safety 	disabled
Zend Memory Manager 	enabled 

Apache/2.2.12 (Ubuntu)
 [2012-07-03 15:02 UTC] pollita@php.net
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.
 [2014-02-06 21:27 UTC] volker at volkers-harfen dot de
Seems to be no bug for me, because documentation writes (at http://www.php.net/manual/en/control-structures.alternative-syntax.php):

Note:
Mixing syntaxes in the same control block is not supported.

This code causes no syntax too, because the if { ... } is in {...} control block:

<?php
if(2>1):
	echo "ABCD";
	{if(4>3){ echo "EFGH"; }}
else:
	echo "123456";
endif;
?>
 [2016-01-15 13:43 UTC] danack@php.net
-Status: Verified +Status: Not a bug
 [2016-01-15 13:43 UTC] danack@php.net
Changing to not a bug as mixing syntaxes in the same control block is not supported.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 07:01:29 2024 UTC