PHP Circular Reference

Source: Internet
Author: User

Yesterday, I encountered a quoted question.

The result is, the first time I also take it for granted.

But the actual results made me surprised. The correct result for [2,3,3]

After some thought and discussion I probably analyzed the flow of its operation and why it was [2,3,3] the result
First, the reference is two or more variables pointing to the same memory address as

$a = 5;
$b = & $a;
So they point to the same memory address whether you change a $ A or a $b value, the other value changes.
Now let me go back to that loop

$arr = [n/a];
foreach ($arr as $key = = $value) {
$value = & $arr [$key];
Print_r ($arr);
}

As you can see from the code, $value points to the address of each element in the array, and if the $value value changes, the value in the array will change.

First cycle
$keyt =0 $value = 1 1 = $arr [0] $value This variable already points to the first element in the $arr array where the position is 1 of the memory location
So when a loop finishes printing the array is

Second cycle
This is the point, because the first loop $value this variable already points to the first element in the $arr array where the position is 1 of the memory location, when the second loop is just beginning, not yet running to $value = & $arr [$key]; this line, at which time the second loop has already put $ Value is changed to 2 because the first loop $value to the position of the array $arr element 1, so when the second loop starts $value the value becomes 2, so the first element in $arr becomes 2 and continues down to $value = & $arr [$key]; 2 = & $arr [1] At this point the address $value to $arr the second element to the memory location, while the second element is assigned a value of 2

The third loop $value already points to the $arr of the second element of the array, so the loop has just begun to $value value is assigned 3, that is, the second element of the array is 3, and then run down $value = & $arr [$key]; 3= & $arr [2] The third element of the vegetarian group is 3, $value the position of the third element that points to the array

The end result of this entire array loop is [2,3,3] the same as if the array adds another 4 to the inevitable [2,3,4,4]

Summarize
At the end of the first loop, a multi-pair memory address mapping is established, and each loop will change the value of the $value corresponding memory address first, that is, the value of the array element, and then $value the memory address of the next element of the array.

PHP Circular Reference

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.