After the first loop $element is still a reference to the last element/value of $array.
You can see that when you use var_dump() instead of print_r()
array(5) {
[0]=>
int(2)
...
[4]=>
&int(2)
}
Note that & in &int(2)
.
With the second loop you assign values to $element. And since it's still a reference the value in the array is changed, too. Try it with
foreach($array as $element)
{
var_dump($array);
}
as the second loop and you'll see.
So it's more or less the same as
$array = range(1,5);
$element = &$array[4];
$element = $array[3];
// and $element = $array[4];
echo $array[4];
(only with loops and multiplication ...hey, I said "more or less" ;-))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…