Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Foreach Pass by Reference: Last Element Duplicating? (Bug?) [duplicate]

After the first foreach loop, $item is still a reference to some value which is also being used by $arr[2]. So each foreach call in the second loop, which does not call by reference, replaces that value, and thus $arr[2], with the new value.

So loop 1, the value and $arr[2] become $arr[0], which is 'foo'.
Loop 2, the value and $arr[2] become $arr[1], which is 'bar'.
Loop 3, the value and $arr[2] become $arr[2], which is 'bar' (because of loop 2).

The value 'baz' is actually lost at the first call of the second foreach loop.

Debugging the Output

For each iteration of the loop, we'll echo the value of $item as well as recursively print the array $arr.

When the first loop is run through, we see this output:

foo
Array ( [0] => foo [1] => bar [2] => baz )

bar
Array ( [0] => foo [1] => bar [2] => baz )

baz
Array ( [0] => foo [1] => bar [2] => baz )

At the end of the loop, $item is still pointing to the same place as $arr[2].

When the second loop is run through, we see this output:

foo
Array ( [0] => foo [1] => bar [2] => foo )

bar
Array ( [0] => foo [1] => bar [2] => bar )

bar
Array ( [0] => foo [1] => bar [2] => bar )

You'll notice how each time array put a new value into $item, it also updated $arr[3] with that same value, since they are both still pointing to the same location. When the loop gets to the third value of the array, it will contain the value bar because it was just set by the previous iteration of that loop.

Is it a bug?

No. This is the behavior of a referenced item, and not a bug. It would be similar to running something like:

for ($i = 0; $i < count($arr); $i++) { $item = $arr[$i]; }

A foreach loop isn't special in nature in which it can ignore referenced items. It's simply setting that variable to the new value each time like you would outside of a loop.


$item is a reference to $arr[2] and is being overwritten by the second foreach loop as animuson pointed out.

foreach ($arr as &$item) { /* do nothing by reference */ }
print_r($arr);

unset($item); // This will fix the issue.

foreach ($arr as $item) { /* do nothing by value */ }
print_r($arr); // $arr has changed....why?

While this may not officially be a bug, in my opinion it is. I think that the problem here is we have the expectation for $item to go out of scope when the loop is exited as it would in a lot of other programming languages. However that doesn't seem to be the case...

This code...

$arr = array('one', 'two', 'three');
foreach($arr as $item){
    echo "$item\n";
}    
echo $item;

Gives the output...

one
two
three
three

As other people already said, you're overwriting the referenced variable in $arr[2] with your second loop, but it's only happening because $item never went out of scope. What do you guys think... bug?