I've seen many questions about this (one of them here)
$a = array('a', 'b', 'c', 'd');
foreach ($a as &$v) { }
foreach ($a as $v) { }
print_r($a);
and I get the answers, you shouldn't be recycling the $v variable name. But..!
Can someone explain to me why this code
$a = array('a', 'b', 'c', 'd');
foreach ($a as &$v) { }
var_dump($a);
prints this (note that the last element is a reference)?
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
&string(1) "d"
}
I expect it to be just a value, only $v should be a reference. In other words, I'm dealing with the same behaviour as described (and well documented) in the quoted question, although I'm not reusing $v variable incorrectly.
foreach
from this discussion if it helps, too: 3v4l.org/5WPsX$b =& $a
, both$a
and$b
reference a common value, and neither of the variables is privileged in any way." They further go on to arrays explaining why this happens but do include the statement "Intuitively this behavior is wrong".