The & amp; pass-through reference problem in PHP. what is the principle of the output result after the result of the foreach loop ?, PHP. In PHP, the result of the foreach loop can help explain the principle of the output result ?, In PHPPHP, the result of the foreach loop can help to explain the reference of the value transfer in PHP. what is the principle of the output result after the result of the foreach loop ?, PHP
In PHP, how does the result of the foreach loop help explain the principle of output results?
The code is as follows:
$ Arr = array ('one', 'two', 'Three ');
Foreach ($ arr as & $ value) {echo 'value: '. $ Value .'
';}
Foreach ($ arr as $ value) {echo 'value: '. $ Value .'
';}
?>
Output result:
Value: one
Value: two
Value: three
Value: one
Value: two
Value: two
The foreach with & for the first time does not change the content of the array ..
Instead, the last loop $ value references the last item of the array (you can test whether the result of the second loop will not change after the first loop ends unset ($ value ),
In your second foreach, $ value variable is also used, which causes a weird problem (you can change the variable, such as $ val, and the output array will not change ).
The second foreach is assigned to $ value, but $ value is the last value of the referenced array,
So
In the first loop, one is assigned to the last value,
The second assignment of two to the last one,
The third time, that is, the last one has been assigned as two in the second loop, so it is still two.
Why ?, The result of the foreach loop can help explain the problem of passing value reference in PHP...