Notes when using phpforeach & (and operator) to reference value assignment

Source: Internet
Author: User
Foreach can easily modify the array unit by adding the $ value before it. When using foreach, pay attention to it. You can also disconnect the reference relationship immediately after processing, and the above will not happen in the future.

Foreach can easily modify the array unit by adding the $ value before it. When using foreach, pay attention to it. You can also disconnect the reference relationship immediately after processing, and the above will not happen in the future.

Foreach can easily modify the array unit by adding & before $ value, such:

PHP code
The Code is as follows:
Foreach ($ arr as $ value ){
$ Value. = '4 ';
}

However, this method can easily cause errors and cannot be found.

The example is more straightforward:

PHP code
The Code is as follows:
$ Arr = array ('A', 'B', 'C ');
$ Arr2 = array ('D', 'E', 'F ');

Foreach ($ arr as $ value) {// $ value or $ val
$ Value. = '4 ';
}

// After processing is complete, we output the template on the page. First, we output $ arr2.
Foreach ($ arr2 as $ value) {// $ value or $ val
// Echo $ value;
}
// Then output $ arr;
Foreach ($ arr as $ value) {// $ value or $ val
Echo $ value, "\ n ";
}
?>

Check whether the output result is the same as expected. The result is:
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 reference.

When the foreach ($ arr as & $ value) array is traversed to the end, the reference relationship is not broken, this is equivalent to the last unit of $ value and $ arr, that is, $ arr [2] reference.

Then to foreach ($ arr2 as $ value), the value of $ value always changes with the value of the array unit. Because the reference relationship is not broken, the value of $ arr [2] also changes. After traversing $ arr2, the value of $ value is f, so the value of $ arr [2] is also f.
The value of $ arr should be:

XML/HTML code
The Code is as follows:
Array
(
[0] => a4
[1] => b4
[2] => f
)

This is different from the final output. Then to foreach ($ arr as $ value), similarly, the value of $ arr [2] also changes with $ value. When the traversal key is 1, that is, when $ arr [1], the value of $ arr [2] is also changed to the value of $ arr [1], that is, b4. Then, when the key is 2, the value of $ arr [2] becomes the value of $ arr [2], that is, b4. Is the output result.

Therefore, you should pay attention to it when using foreach reference. You can also disconnect the reference relationship immediately after processing, and the above will not happen in the future.

PHP code
The Code is as follows:
Foreach ($ arr as $ value ){
$ Value. = '4 ';
}
Unset ($ value );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.