Problems with & of key values in the Foreach Loop in PHP

Source: Internet
Author: User
Tags foreach loop in php
The code is as follows:

$a = [1, 2, 3];foreach($a as $key => &$value) {    $value = 5;}var_dump($a);

The final output results are as follows:

array(3) { [0]=> int(5) [1]=> int(5) [2]=> &int(5) }

Question: Why do I get an address character before the last key value?

Reply content:

The code is as follows:

$a = [1, 2, 3];foreach($a as $key => &$value) {    $value = 5;}var_dump($a);

The final output results are as follows:

array(3) { [0]=> int(5) [1]=> int(5) [2]=> &int(5) }

Question: Why do I get an address character before the last key value?

The & represented in this var_dump you can change its value with a different variable.
Like you do.

$b = &$a[0];var_dump($a);

When printed here, the & symbol appears in the first key value, indicating that you can change its value with other variables ($b).

Let & 's take a look at the basic usage.

$b = $c = 1;$a = &$b;$a = 2;echo "b=$b,c=$c\n";$a = &$c;$a = 100;$a = 200;echo "b=$b,c=$c\n";unset($a);$a = 500;echo "b=$b,c=$c\n";

After a $ A has been specified as a reference to $b, changing $ A will change the value of the corresponding $b unless it is specified again as another reference, or unset ($a).

For you this foreach, is the same truth, you take the loop apart, this is the way:

$value = &$a[0];$value = 5;$value = &$a[1];$value = 5;$value = &$a[2];$value = 5;

The loop runs to the last time, $value is a reference to $a[2], so it is equivalent to this form:

$a[0] = 5;$a[1] = 5;$value = &$a[2];$a[2] = 5;var_dump($a);

It is not difficult to understand why Var_dump will print a & symbol on the last key-value pair.

In general, this will not cause any major problems, but because of the scope of the foreach problem, will cause the loop exit after the $value can still be used, so there will be some strange bugs.

Like this.

$a = [1, 2, 3];foreach($a as $key => &$value) {    $value = $value+5;}var_dump($a); // [6,7,8]foreach($a as $value){}var_dump($a); // [6,7,7]

After adding an empty foreach, it turns out that $ A is unreasonable, and the bug is not surprising, because the $value in the empty foreach is the reference to the top &a[2].

The empty foreach is equivalent to this:

$value = &$a[2];$value = $a[0];$value = $a[1];$value = $a[2];

Notice that because $value is a reference to $a[2], the above rewrite is this:

$a[2] = $a[0];$a[2] = $a[1];$a[2] = $a[2];

The empty foreach is constantly changing $a[2], and since $a[2] has become the value of $a[1], $a[2] = $a [2]; no effect, value or $a[1];

This strange bug is due to the scope of the variable $value, so either change the name, or first give the $value to unset off.

$a = [1, 2, 3];foreach($a as $key => &$value) {    $value = $value+5;}var_dump($a);// 要么unsetunset($value);// 要么foreach里不要用上面同名的$value,改为$value2222foreach($a as $value2222){}var_dump($a);

First of all, I would like to say that this is a very, very good question. I believe that the main problem is not the separation of reference and non-reference.
I did a little bit of two Tests first:

$a = [1, 2, 3];foreach($a as $key => &$value) {    $value = 5;}foreach($a as $key => $value) {    $value = 6;}var_dump($a);

What do you guess the output is?

array(3) {  [0]=>  int(5)  [1]=>  int(5)  [2]=>  &int(6)}

Another test is:

$a = [1, 2, 3];foreach($a as $key => &$value) {    $value = 5;}foreach($a as $key => $value2) {    $value2 = 6;}var_dump($a);

This time the output looks a lot more normal:

array(3) {  [0]=>  int(5)  [1]=>  int(5)  [2]=>  &int(5)}

So, the problem should be a hole in the PHP global variable.

  • Related Article

    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.