A problem and solution to be noted when using references in a PHP foreach _php instance

Source: Internet
Author: User

I. Problems
Let's look at one example:

<?php
$ar = Array (1, 2, 3);
Var_dump ($ar);
foreach ($ar as & $v) {}
foreach ($ar as $v) {}
Var_dump ($ar);
?>
The output is:

Array (3) {
[0]=>
Int (1)
[1]=>
Int (2)
[2]=>
Int (3)
}
Array (3) {
[0]=>
Int (1)
[1]=>
Int (2)
[2]=>
&int (2)
}
??? Why is the value of the last element of the array changed without an assignment?

I have found this problem, at first thought it was a PHP bug, threw it, not the use of a reference in foreach is Nothing, with a foreach $k => $v then $ar [$k] To change the original array, slightly loss of efficiency.

Second, analysis

Today, it took some time to read the article in the reference, which is a little bit more clear, the original is like this:

At the beginning of the first use of a referenced foreach, $v point to the storage space $ar [0], where 1 is stored, and at the end of the foreach, $v points to $ar [2] storage space, where 3 is stored. To begin with the second foreach, note that unlike the first foreach, the second foreach does not use a reference, and that is the assignment, which assigns the value of the $ar to the $v in turn. When you go to the first element, you assign the $ar [0] to the $v. The problem is here, because just after the first foreach has been executed, $v is not a new variable, but a reference that already exists that points to $ar [2], so that when the $v is assigned, $ar [0] = 1 is written to the actual storage space $ar [2], equivalent to the $ar [2] to assign a value. So, the result of the second foreach execution is that the last element of the array becomes the value of the penultimate element. A detailed schematic is available in the reference article 2.

If this is a mistake, then the reason for the error is the use of reference variables. When referring to variable points and other variables, changing the value of a reference variable will of course affect other variables that he points to. Everyone knows it alone, but in this foreach example, coincidentally, the same variable was used two times, the previous time is the identity of the reference, the latter is the normal variable identity, resulting in an unexpected effect. PHP developers also believe that this situation is caused by language features, not bugs. Indeed, one way to fix this problem is to specifically deal with foreach, and the other is to limit the scope of $v in foreach, both of which are incompatible with the language features of the current PHP, and developers are unwilling to change it, but still use Warning in official documents. Ming.

Third, the solution

Simple, but not perfect, is after using the quoted foreach, unset off $v, the beginning of the example to:

<?php
$ar = Array (1, 2, 3);
Var_dump ($ar);
foreach ($ar as & $v) {}
Unset ($v);
foreach ($ar as $v) {}
Var_dump ($ar);
?>
Run Result:

Array (3) {
[0]=>
Int (1)
[1]=>
Int (2)
[2]=>
Int (3)
}
Array (3) {
[0]=>
Int (1)
[1]=>
Int (2)
[2]=>
Int (3)
}


Reference

Bug #29992 foreach by reference corrupts the array:https://bugs.php.net/bug.php?id=29992
References and Foreach:http://schlueters.de/blog/archives/141-references-and-foreach.html

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.