A problem that foreach uses to refer to

Source: Internet
Author: User
Tags foreach php code vars

foreach can easily modify an array of cells by adding & before $value, such as:

PHP code
  1. foreach($arr as &$value) {
  2. $value . = ' 4 ';
  3. }

But this usage is also easy to make mistakes, and it is not easy to find.
See the example more straightforward:

PHP Code
  1. <?php
  2. $arr = array(' A ',' B ', 'C ');
  3. $arr 2 = array(' d ', ' e ', ' f ');
  4. foreach($arr as &$value) {//accustomed to using $value or $val
  5. $value . = ' 4 ';
  6. }
  7. We have finished processing in the page template output, first output $arr2
  8. foreach($arr 2 as $value) {//accustomed to using $value or $val
  9. //echo $value;   
  10. }
  11. And then output $arr like this;   
  12. foreach($arr as $value) {//accustomed to using $value or $val
  13. echo $value, "n";
  14. }
  15. ?>

Let's see if the output turns out to be the same as expected. Here the result is:

A4
B4
B4

The result is different from what I expected, and this is the problem that the citation causes.
At the end of a foreach ($arr as & $value) array, the reference relationship is not disconnected, which is equivalent to the last unit of the $value and $arr that is $arr [2] reference.

To foreach ($arr 2 as $value), the value of the $value has been changed with the value of the array cell, and the value of $arr [2] changes as the reference relationship is not broken. All the time until the $ARR2 traversal, this is the $value value is F, so $arr[2] value is also f.
Then the value of $arr should be:

Array
(
[0] => A4
[1] => B4
[2] => F
)

This is not the same as the final output we see. Again to foreach ($arr as $value), the value of $arr [2] also changes with $value, and when traversing to key 1, or $arr[1], the value of $arr [2] becomes the value of $arr [1], which is B4. Then the value of the key 2 o'clock $arr [2] becomes the value of the $arr [2], which is B4. Is the result of the output.

So be aware when foreach uses the reference. You can also disconnect the referral relationship as soon as you have finished processing it.

php code
  1. foreach ( $arr  as  &< Span class= "VARs" $value ) {  
  2.   $value  .=  ' 4 '    /span>
  3.   
  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.