php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53860 unexpected behaviour in 'foreach' syntax checking
Submitted: 2011-01-27 21:41 UTC Modified: 2011-01-28 15:25 UTC
From: soriasoft at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3.5 OS: Linux
Private report: No CVE-ID: None
 [2011-01-27 21:41 UTC] soriasoft at gmail dot com
Description:
------------
I found and unexpected behavior in the syntax checking. When you write a ';' after the open brace, the parser doesn't display an error or notice message, and executes the code with the last element of the array. I test this in php5.3 and php5.2.

This "innocent" ; is realy hard to debug.

The enviroments:

joac@pepino ~/ReposGcoop $ php -v
PHP 5.3.3 with Suhosin-Patch (cli) (built: Jul 25 2010 12:50:19) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

debdev@jaula:~/www$ php -v
PHP 5.2.6-1+lenny9 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug  4 2010 03:25:57) 
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
    with Xdebug v2.0.3, Copyright (c) 2002-2007, by Derick Rethans


Test script:
---------------
<?php
function show_array(&$data_array){
    foreach ($data_array as $key=>$value);
    {   
        print "$key => $value\n";
        if ( is_array( $value ) ){
           show_array1( $value );
           print "End of nested Array\n";
        }   
    }   
};
$evil_array = array (
        'item1' => array ( 'item1_1' => array( 'item1_1_1' => 1, 'item1_1_2' => 2 ),
        'item1_2' => array( 'item1_2_1' => 3,),
        ),  
    'item2' => '4'); 

show_array($evil_array);
?>
Output:

joac@pepino ~/ReposGcoop $ php bugphp.php 
item2 => 4


Expected result:
----------------
I expect some error or notice for this syntax error.

Actual result:
--------------
joac@pepino ~/ReposGcoop $ php bugphp.php 
item2 => 4

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-27 22:21 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-01-27 22:21 UTC] rasmus@php.net
There is no syntax error there though.

foreach(...);  is a valid loop construct and { expr; } is also a valid construct 
on its own.
 [2011-01-28 15:25 UTC] soriasoft at gmail dot com
ok, but, $key and $value(the iteration variables) still "living" out of the foreach loop code, and make this really hard to debug. In other languages, this don't happens.
May be for you, is not an error, but some notice of this isn't a bad idea.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 09:01:34 2025 UTC