Edit report at https://bugs.php.net/bug.php?id=29992&edit=1

 ID:                 29992
 Updated by:         johan...@php.net
 Reported by:        fletch at pobox dot com
 Summary:            foreach by reference corrupts the array
 Status:             Bogus
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   linux
 PHP Version:        5.0.1
 Block user comment: N
 Private report:     N

 New Comment:

daniel,

unsetting it before might also cause a wrong result. Simple example:
<?php
function search_element($array, &$var) {
    foreach ($array as &$var) {
        if ($condition) {
            return true;
        }
    }
}
$array=array(1,2,3,4,5,6);
$var = null;
search_element($array, $var);
?>

This is simplified and probably no good architecture but such designs might 
make sense in some situations and breaking that adds a larger inconsistency 
than the current "surprising but consistent" behavior.


martijn,

Of course there is a reference at the end, absolutely expected and consistent 
with the language.


Previous Comments:
------------------------------------------------------------------------
[2011-07-13 06:48:49] martijn at twotribes dot com

To elaborate of why I strongly feel this is a bug and not a 'feature':

$a = array('a', 'b', 'c', 'd');
foreach ($a as &$v) { }
var_dump($a);

One would expect that every element of $a is a string. Well it was, up until I 
did that foreach-with-reference. That changed the last element into string 
reference:

array(4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  &string(1) "d"
}

The PHP guys can claim that this is correct behavior all they want, but it is 
fundamentally wrong from a design perspective that an array changes, without 
doing anything to its elements.

------------------------------------------------------------------------
[2011-07-13 06:37:58] martijn at twotribes dot com

Can someone please promote this "bogus" status to an actual bug? It completely 
baffles me why something so obviously wrong, has been present in PHP for almost 
7(!) years. Come on guys, fix this!

------------------------------------------------------------------------
[2011-07-01 06:41:47] daniel at milde dot cz

I agree with Looris. This behaviour is dangerous and no one expects it.

I suggest unsetting the variable BEFORE each loop. This should cause no harm.

------------------------------------------------------------------------
[2010-11-27 14:59:24] johan...@php.net

I explained this with some pictures in my blog: 
http://schlueters.de/blog/archives/141-References-and-foreach.html

And no, unsetting after the loop is no option as it is inconsistent with other 
loops and there are valid reasons for keeping it after the loop. Something 
along the lines of

foreach ($array as &$item) {
    if ($item == "foo") {
        break;
    }
}

$item = "bar";

------------------------------------------------------------------------
[2010-11-27 10:28:58] tarama at hotmail dot com

I highly agree with looris. I had to patch each use of hundreds of foreach 
loops in my code due to this weird behaviour. The unset() tips at the end of 
each loop does the job, but from my point of view it's really not logical. It 
caused really hard to find bugs in my code and I prefer to not imagine how many 
PHP scritps are running on the web with the same "bug" silently breaking things.

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=29992


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=29992&edit=1

Reply via email to