PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

Bug #35106 nested foreach fails when array variable has a reference
Submitted:4 Nov 2005 4:57pm UTC Modified: 10 Jan 2007 4:01pm UTC
From:olympnn at nm dot ru Assigned to:dmitry
Status:Closed Category:Scripting Engine problem
Version:5CVS-2005-11-05 (cvs) OS:*
Votes:5 Avg. Score:3.6 ± 0.8 Reproduced:5 of 5 (100.0%)
Same Version:1 (20.0%) Same OS:1 (20.0%)
View/Vote Developer Edit Submission

[4 Nov 2005 4:57pm UTC] olympnn at nm dot ru
Description:
------------
When I execute the code, it seems that the outer foreach is executed
only once. When I comment any of lines marked *, the (outer) foreach
executes twice.

This bug seems to be very like to bug # 21702, but here the situation is
much more strange: I do not use $b in foreaches at all! So it's very
strange that the foreach behavior differs when the array was referenced
sometime or not.

Thanks.

Reproduce code:
---------------
<?
$a=array("1","2");
$b=&$a;//*
foreach($a as $i){
    print($i);
    foreach($a as $p);//*
}
?>

Expected result:
----------------
12

Actual result:
--------------
1
[5 Nov 2005 4:37pm UTC] sniper@php.net
Verified. Dmitry, check this out.
[10 Feb 2006 8:48pm UTC] brion at wikimedia dot org
Note that the same happens with global variables accessed 
from a function with the 'global' statement. Since globals 
work via references, this seems to be the same issue.

This was closed as "won't fix" in 2003:
http://bugs.php.net/bug.php?id=26396

I sincerely hope the opinion has changed in the intervening 
time, as it's pretty clearly poor behavior.
[10 Jan 2007 4:01pm UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_2

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC