php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #838 Parser error in nested if
Submitted: 1998-10-09 05:47 UTC Modified: 1998-10-09 08:20 UTC
From: agarcia at at4 dot net Assigned:
Status: Closed Package: Parser error
PHP Version: 3.0.5 OS: win95 and linux 2.035 RH5.0
Private report: No CVE-ID: None
 [1998-10-09 05:47 UTC] agarcia at at4 dot net
This script causes a parser error:
<?
$a = $b = 2;
if ($a==2):
	if ($b==2) print "a=2 b=2<br>";
elseif ($a=3):
	print "a=3<br>"
endif;
?>
This script also fails:
<?
$a = $b = 2;
if ($a==2):
	if ($b==2) {print "a=2 b=2<br>"};
elseif ($a=3):
	print "a=3<br>"
endif;
?>

Based in the fact that this code isn't correct:

if ($b==2) print "a=2 b=2<br>";
elseif ($a=3): 
     print "a=3<br>";
endif;

i think it's clear the elseif ($a=3):
belongs to the previous if sentence.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1998-10-09 08:20 UTC] zeev
How is it clear?  Because you've indented it that way? :)

It's not clear at all, and in fact, the solution for the dangling else    
shift/reduce problem clearly states that the else belongs to the closest  
IF.   
   
PHP makes no distinction between if's with braces, no braces, or colons,  
and it doesn't pay attention to your indentation either.  As soon as it   
sees the elseif, it expects it to match the inner IF sentence, and since  
it doesn't, it fails.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 15:01:32 2024 UTC