I have a doubt in php foreach iteration.
Please look my below code.
CODE :
$arr=array(1,2,3);
echo '$arr value : '. $arr;
echo '<br>';
foreach($arr as $arr){
echo $arr.'<br>';
}
echo '$arr value : '. $arr;
OUTPUT :
$arr value : Array
1
2
3
$arr value : 3
While iterating array i used same array name to value key foreach($arr as $arr)
but it works fine. How it is working?.
Why it doesn't override the array value while iterating?
I want to know how foreach iteration is working.
Please Help me!
Thanks in advance Logan
Why it doesn't override the array value while iterating?
foreach will work on a copy of the original array so things you change in the foreach loop will not change the values you are looping over.
PHP uses a copy on write
or lazy copy
mechanism with reference counting for memory handling.
In the first stage of foreach iteration, $arr
is "soft copied" (no actual copy, but only the refcount
of the zval
of $array
is increased). checkout variable container
and label
of PHP variables.
In our case,
$arr
variable, and the first value of array is fetched from the reference point.$arr
foreach($arr as $arr)$arr
and continues.$arr
is changed inside loop, you cannot print that array outside loop using $arr
variable.Hope you can understand...
For an in-depth study checkout http://www.php.net/manual/en/internals2.opcodes.fe-fetch.php and http://www.php.net/manual/en/internals2.opcodes.fe-reset.php
Interestingly you can play with this too...
current()
inside a foreach loop will always returns the same element!!!
Code
$source = array(10, 20, 30);
foreach ($source as $value) {
echo 'Value is ', $value, ', and current() is ', current($source), '<br>';
}
Output
Value is 10, and current() is 20
Value is 20, and current() is 20
Value is 30, and current() is 20
foreach ($arr as $arr) {
// code
}
This may be confusing to you because your mental model of how this works is:
$arr
and advances the array pointer$arr
// code
What it actually does though is this (or at least an approximate simplification of it):
foreach
operation, taking the array value that is associated with $arr
var iterator
var iterator
and advances the iterator$arr
in your script// code
In other words, it does not take the next value from $arr
on every iteration, it only used this once to get the initial iterator. The actual thing that it iterates over is stored internally and is not associated with $arr
anymore. That means $arr
is free for other purposes in your script and PHP is happy to reuse it on each iteration. After the loop is done, the last element remains in $arr
.
If you know anonymous functions, maybe this illustrates better why this works the way it works:
foreach($arr, function ($arr) {
// code
});
The implementation of such a foreach
function can be simply this:
function foreach(array $array, callable $code) {
while (list(, $value) = each($array)) {
$code($value);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With