164

I just had some very strange behavior with a simple php script I was writing. I reduced it to the minimum necessary to recreate the bug:

<?php

$arr = array("foo",
             "bar",
             "baz");

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

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

?>

This outputs:

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

Is this a bug or some really strange behavior that is supposed to happen?

John Smith
  • 1,750
  • 3
  • 18
  • 31
regality
  • 6,496
  • 6
  • 29
  • 26

5 Answers5

174

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.

Community
  • 1
  • 1
animuson
  • 53,861
  • 28
  • 137
  • 147
  • Sweet, that makes sense. Is this defined behavior or should I file a bug report? – regality Nov 22 '11 at 00:31
  • 4
    I have a slight pedantic correction. `$item` is not a reference to `$arr[2]`, the value contained by `$arr[2]` is a reference to the value referred to by `$item`. To illustrate the difference, you could also unset `$arr[2]`, and `$item` would be unaffected, and writing to `$item` wouldn't affect it. – Paul Biggar Nov 22 '11 at 05:58
  • 2
    This behavior is complex to understand and may lead to problems. I keep this as one of my favorites to show my students why they should avoid (as long as they can) things "by reference". – Olivier Pons Nov 22 '11 at 08:15
  • 1
    Why does `$item` not go out of scope when the foreach loop is exited? This seems like a closure problem? – jocull Nov 25 '11 at 17:24
  • 6
    @jocull: IN PHP, foreach, for, while, etc do not create their own scope. – animuson Nov 25 '11 at 18:24
  • @regality: This is by design. – BoltClock Jan 18 '12 at 18:37
  • 1
    @jocull, PHP doesn't have (block) local variables. One of the reason it annoys me. – Qtax Apr 13 '13 at 11:28
  • is there a way to 'reset the var'? so I can use the $item again? – Rafael Moni Apr 09 '18 at 18:15
  • @RafaelMoni Easy. Just add `unset($item);` after the first foreach loop, as recommended in the PHP manual: http://php.net/manual/en/control-structures.foreach.php – Sean the Bean Oct 12 '18 at 14:08
33

$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?
Michael Leaney
  • 753
  • 4
  • 10
4

It's very surprising behavior, but widely declared as not a bug. PHP (at time of writing) doesn't have block level variables. If you expect for $item to go out of scope when the loop is exited you will find that it does not.

Here is a simplified example:

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

Which outputs:

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.

jocull
  • 20,008
  • 22
  • 105
  • 149
  • 4
    1) Not a bug. It's already called out in the [manual](http://php.net/manual/en/control-structures.foreach.php) and dismissed in a number of bug reports as intended. 2) Doesn't really answer the question... – BoltClock Nov 25 '11 at 17:49
  • It caught me out not because of the scope issue, I expected $item to remain around after the initial foreach, but I didn't realize that foreach UPDATES the variable instead of REPLACING it. e.g the same as running unset($item) before the second loop. Note that the unset does not clear the value (and thus the last element in the array) it simply removes the variable. – Programster Dec 06 '13 at 12:34
  • Unfortunately, PHP does not create a new scope for loops or `{}` blocks in general. This is how the language works – Fabian Schmengler Jul 09 '15 at 07:40
0

The correct behaviour of PHP sould be a NOTICE error in my oppinion. If a referenced variable created in a foreach loop is used outside the loop it should cause a notice. Very easy to fall for this behaviour, very difficult to spot it when it happened. And no developer is going to read the foreach documentation page, it's not a help.

You should unset() the reference after your loop to avoid this sort of issue. unset() on a reference will just remove the reference without harming the original data.

John
  • 7,507
  • 3
  • 52
  • 52
0

that's because you use by ref directive (&). last value will be replace by the second loop and it corrupt your array. the simplest solution is to use different name for second loop:

foreach ($arr as &$item) { ... }

foreach ($arr as $anotherItem) { ... }
Amir Surnay
  • 384
  • 2
  • 11