Outdoor research php foreach issues to pay attention to when using & and operator reference assignment

WBOY
Release: 2016-07-29 08:41:51
Original
931 people have browsed it

foreach can easily modify the elements of the array by adding & before $value, such as:
PHP code

Copy code The code is as follows:


foreach($arr as $value){
$value . = '4';
}


But this usage is also easy to cause errors, and it is not easy to find.
Look at the example to make it more straightforward:
PHP code

Copy the code The code is as follows:


$arr = array('a','b','c');
$arr2 = array('d', 'e', ​​'f');
foreach($arr as $value){//Used to use $value or $val
$value .= '4';
}
//Handle both After we finish the output in the page template, first output $arr2
foreach($arr2 as $value){//Usually use $value or $val
//echo $value;
}
//Then output $arr;
foreach($arr as $value){//Used to use $value or $val
echo $value, "n";
}
?>


Let's see if the output result is the same as expected. The result here is:

Copy the code The code is as follows:


XML/HTML code
a4
b4
b4


The result is different from what I expected. This is a problem caused by references.
When foreach($arr as &$value) traverses the array to the end, the reference relationship is not broken. This is equivalent to the reference of the last unit of $value and $arr, which is $arr[2].
Going to foreach($arr2 as $value), the value of $value keeps changing with the value of the array unit. Since the reference relationship is not broken, the value of $arr[2] also changes accordingly. Until $arr2 is traversed, the value of $value is f, so the value of $arr[2] is also f.
At this time, the value of $arr should be:
XML/HTML code

Copy code The code is as follows:


Array
(
[0] => a4
[1] => b4
[ 2] => f
)


This is different from the final output we see. Then go to foreach($arr as $value). In the same way, the value of $arr[2] also changes with $value. When the key is 1, that is, $arr[1], $arr[2] The value also becomes the value of $arr[1], which is b4. Then when iterating to key 2, the value of $arr[2] becomes the value of $arr[2] again, which is b4. It is the result of output.
So be careful when using references in foreach. You can also disconnect the reference relationship immediately after processing, so that the above situation will not happen later.
PHP code

Copy code The code is as follows:


foreach($arr as $value){
$value .= '4';
}
unset($value);

The above introduces the issues that should be paid attention to when using & and operator reference assignment in outdoor research php foreach, including the content of outdoor research. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template