If I iterate through an array twice, once by reference and then by value, PHP will overwrite the last value in the array if I use the same variable name for each loop. This
Here's an explanation from the man himself:
$y = "some test"; foreach ($myarray as $y) { print "$y\n"; }
Here
$y
is a symbol table entry referencing a string containing "some test". On the first iteration you essentially do:$y = $myarray[0]; // Not necessarily 0, just the 1st element
So now the storage associated with
$y
is overwritten by the value from$myarray
. If$y
is associated with some other storage through a reference, that storage will be changed.Now let's say you do this:
$myarray = array("Test"); $a = "A string"; $y = &$a; foreach ($myarray as $y) { print "$y\n"; }
Here
$y
is associated with the same storage as$a
through a reference so when the first iteration does:$y = $myarray[0];
The only place that "Test" string can go is into the storage associated with
$y
.