Use of the middle-key in the foreach loop in php

Source: Internet
Author: User
Tags foreach loop in php
The code is as follows: {code...} the final output result is as follows: {code...} question: why does the Get address character appear before the last key value? The code is as follows:

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

The output result is as follows:

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

Q: Why does the Get address character appear 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 output result is as follows:

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

Q: Why does the Get address character appear before the last key value?

The & in this var_dump indicates that you can use another variable to change its value.
For example

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

The & symbol appears in the first key value when printing, indicating that you can use other variables ($ B) to change its value.

Let's take a look&.

$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 is specified as a reference of $ B, changing $ a is to change the value of $ B unless it is specified as another reference or unset ($.

The same is true for your foreach. you split the loop in one step, just like this:

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

When the loop is run to the end, $ value is a reference of $ 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 prints an & symbol on the last key value.

Generally, this will not cause any major problems, but due to the scope of foreach, $ value can still be used after the loop exits, so some incredible bugs may occur.

For example

$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, $ a becomes unreasonable. this bug is incredible, the reason is that the $ value in the blank foreach is the reference of the & a [2] at the top.

The null foreach is equivalent to the following:

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

Note that because $ value is a reference of $ a [2], the above rewrite is as follows:

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

The empty foreach keeps changing $ a [2], and because $ a [2] has become the value of $ a [1, therefore, $ a [2] = $ a [2]; has no effect. The value is still $ a [1].

This incredible bug is caused by the scope of the $ value variable, so either change the name or give $ value to unset first.

$ A = [1, 2, 3]; foreach ($ a as $ key => & $ value) {$ value = $ value + 5;} var_dump ($ ); // either unsetunset ($ value); // do not use the $ value with the same name in foreach; change it to $ value2222foreach ($ a as $ value2222) {} var_dump ($ );

First, I want to say that this isVeryOkay. I believe that the subject does not clearly distinguish between reference and non-reference.
I did two tests first:

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

Guess what 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 output looks much more normal:

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

Therefore, this problem should be a pitfall of php global variables.

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.