I have this code:
$a = array ('zero','one','two', 'three');
foreach ($a as &$v) {
}
foreach ($a as $v) {
echo $v.PHP_EOL;
}
Can somebody explain why the output is: zero one two two .
From zend certification study guide.
I had to spend a few hours to figure out why a[3] is changing on each iteration. This is the explanation at which I arrived.
There are two types of variables in PHP: normal variables and reference variables. If we assign a reference of a variable to another variable, the variable becomes a reference variable.
for example in
$a = array('zero', 'one', 'two', 'three');
if we do
$v = &$a[0]
the 0th element ($a[0]
) becomes a reference variable. $v
points towards that variable; therefore, if we make any change to $v
, it will be reflected in $a[0]
and vice versa.
now if we do
$v = &$a[1]
$a[1]
will become a reference variable and $a[0]
will become a normal variable (Since no one else is pointing to $a[0]
it is converted to a normal variable. PHP is smart enough to make it a normal variable when no one else is pointing towards it)
This is what happens in the first loop
foreach ($a as &$v) {
}
After the last iteration $a[3]
is a reference variable.
Since $v
is pointing to $a[3]
any change to $v
results in a change to $a[3]
in the second loop,
foreach ($a as $v) {
echo $v.'-'.$a[3].PHP_EOL;
}
in each iteration as $v
changes, $a[3]
changes. (because $v
still points to $a[3]
). This is the reason why $a[3]
changes on each iteration.
In the iteration before the last iteration, $v
is assigned the value 'two'. Since $v
points to $a[3]
, $a[3]
now gets the value 'two'. Keep this in mind.
In the last iteration, $v
(which points to $a[3]
) now has the value of 'two', because $a[3]
was set to two in the previous iteration. two
is printed. This explains why 'two' is repeated when $v is printed in the last iteration.
reference variable
is the reference
, in this example $v
. $a
, on the other hand, always remains a normal variable and is not a reference variable in my point of view. Therefore, PHP is also not "smart" in that it knows that a variable has no references to it, because it does not really matter for the variable itself if it is referenced somewhere. The important thing to understand is that $v
is a reference and not a variable. Otherwise a very good explanation.
Nov 22, 2018 at 18:36
$v
is leaked from the first loop. This would be a really nice trick for intentional code obfuscation... As usual, the fix is to unset($v)
to get rid of the reference or move that code in a new function so that $v gets its own scope and doesn't leak to following code.
Aug 16, 2022 at 13:08
Because on the second loop, $v
is still a reference to the last array item, so it's overwritten each time.
You can see it like that:
$a = array ('zero','one','two', 'three');
foreach ($a as &$v) {
}
foreach ($a as $v) {
echo $v.'-'.$a[3].PHP_EOL;
}
As you can see, the last array item takes the current loop value: 'zero', 'one', 'two', and then it's just 'two'... : )
$v gets the item [0](zero). $a[3] is now zero $v gets the item [1](one). $a[3] is now one $v gets the item [2](two). $a[3] is now two $v gets the item [3](two). $a[3] is now two
unset($item);
after the first foreach loop to avoid this problem. php.net/manual/en/control-structures.foreach.php
Oct 12, 2018 at 14:09
First loop
$v = $a[0];
$v = $a[1];
$v = $a[2];
$v = $a[3];
Yes! Current $v
= $a[3]
position.
Second loop
$a[3] = $v = $a[0], echo $v; // same as $a[3] and $a[0] == 'zero'
$a[3] = $v = $a[1], echo $v; // same as $a[3] and $a[1] == 'one'
$a[3] = $v = $a[2], echo $v; // same as $a[3] and $a[2] == 'two'
$a[3] = $v = $a[3], echo $v; // same as $a[3] and $a[3] == 'two'
because $a[3]
is assigned by before processing.
I got here just by accident and the OP's question got my attention. Unfortunately I do not understand any of the explanations from the top. Seems to me like everybody knows it, gets it, accetps it, just cannot explain.
Luckily, a pure sentence from PHP documentation on foreach makes this completely clear:
Warning: Reference of a
$value
and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().
I think this code show the procedure more clear.
<?php
$a = array ('zero','one','two', 'three');
foreach ($a as &$v) {
}
var_dump($a);
foreach ($a as $v) {
var_dump($a);
}
Result: (Take attention on the last two array)
array(4) {
[0]=>
string(4) "zero"
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
&string(5) "three"
}
array(4) {
[0]=>
string(4) "zero"
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
&string(4) "zero"
}
array(4) {
[0]=>
string(4) "zero"
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
&string(3) "one"
}
array(4) {
[0]=>
string(4) "zero"
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
&string(3) "two"
}
array(4) {
[0]=>
string(4) "zero"
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
&string(3) "two"
}
This question has a lot of explanations provided, but no clear examples of how to solve the problem that this behavior causes. In most cases, you'll probably want the following code in your pass by reference foreach
.
foreach ($array as &$row) {
// Do stuff
}
// Unset to remove the reference
unset($row);
unset($row[$k]);
instead.
This :
$a = array ('zero','one','two', 'three');
foreach ($a as &$v) {
}
foreach ($a as $v) {
echo $v.PHP_EOL;
}
is the same as
$a = array ('zero','one','two', 'three');
$v = &$a[3];
for ($i = 0; $i < 4; $i++) {
$v = $a[$i];
echo $v.PHP_EOL;
}
OR
$a = array ('zero','one','two', 'three');
for ($i = 0; $i < 4; $i++) {
$a[3] = $a[$i];
echo $a[3].PHP_EOL;
}
OR
$a = array ('zero','one','two', 'three');
$a[3] = $a[0];
echo $a[3].PHP_EOL;
$a[3] = $a[1];
echo $a[3].PHP_EOL;
$a[3] = $a[2];
echo $a[3].PHP_EOL;
$a[3] = $a[3];
echo $a[3].PHP_EOL;
I found this example also tricky. Why that in the 2nd loop at the last iteration nothing happens ($v stays 'two'), is that $v points to $a[3] (and vice versa), so it cannot assign value to itself, so it keeps the previous assigned value :)
Because if you create a reference to a variable, all names for that variable (including the original) BECOME REFERENCES.
unset($your_used_reference);
every time you use aforeach($var as &$your_used_reference)
!