php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24758 foreach() is broken
Submitted: 2003-07-22 16:33 UTC Modified: 2003-07-24 12:34 UTC
From: vma1 at abv dot bg Assigned: Zeev (profile)
Status: Closed Package: Arrays related
PHP Version: 5CVS-2003-07-22 (dev) 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: vma1 at abv dot bg
New email:
PHP Version: OS:

 

 [2003-07-22 16:33 UTC] vma1 at abv dot bg
Description:
------------
The last CVS version php5-200307221730 has broken handling of arrays in foreach() structure. When enumerating array keys the values cannot be accessed.

Reproduce code:
---------------
$list = array ();
$list ["0"] = "abc";
foreach ($list as $key => $unused) {
	printf ("%s<br>", $list [$key]);
}


Expected result:
----------------
abc


Actual result:
--------------
Notice: Undefined index: 0 in /usr/local/apache/site/htdocs/bug.php on line 5


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-22 16:53 UTC] helly@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip

For me it works, so i think my latest commit fixed it.

$ php -r '$ar=array(0=>"a",1=>"b");foreach($ar as $k=>$v) echo "$k=>$v\n";'
0=>a
1=>b
 [2003-07-22 18:17 UTC] vma1 at abv dot bg
No. It is still broken. But try my script exactly the way I've written it. Your version works correctly, but not mine.
 [2003-07-22 18:29 UTC] vma1 at abv dot bg
Actually very small changes in the script make the problem go away. E.g. change index from "0" to 0, or replace printf with echo, or change array initialization from
$list = array ();$list ["0"] = "abc";
to
$list = array ("0" => "abc");

and the problem goes away.
 [2003-07-22 18:33 UTC] helly@php.net
Weel i see what you mean:

php -r '$ar=array();$ar["0"]="a";$ar["1"]="b";foreach($ar as $k=>$v) echo "$k=>".$ar[$k]."\n";'
Notice: Undefined index:  0 in Command line code on line 1
0=>
Notice: Undefined index:  1 in Command line code on line 1
1=>

php -r '$ar=array(0=>"a",1=>"b");foreach($ar as $k=>$v) echo "$k=>".$ar[$k]."\n";'
0=>a
1=>b

php -r '$ar=array();$ar["0"]="a";$ar["1"]="b";foreach($ar as $k=>$v) echo "$k=>$v\n";'
0=>a
1=>b
 [2003-07-24 12:34 UTC] zeev@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 17:01:33 2024 UTC