PHP source debugging foreach Strange Phenomenon

Source: Internet
Author: User
Tags php source code
$array = Array (1,2,3,4);
foreach ($array as $key = = $value) {
$value = & $array [$key];
Echo current ($array);
}
Output 2 2 3 3
This piece of code to my confused, I hope the great God through the PHP source code to explain.

Reply content:

$array = Array (1,2,3,4);
foreach ($array as $key = = $value) {
$value = & $array [$key];
Echo current ($array);
}
Output 2 2 3 3
This piece of code to my confused, I hope the great God through the PHP source code to explain.

First of all, to understand this problem, we have to break the loop first and then see how the values of the array will change:

1) First cycle

php$value = $array[0];$value = &$array[0]; // $value现在变成了$array[0]的引用了

这时的$array = array(1, 2, 3, 4);

2) Second cycle

php$value = $array[1]; // 由于$value引用$array[0],此语句相当于$array[0] = $array[1],也就是$array[0] = 2$value = &$array[1]; // $value现在变成了$array[1]的引用了

这时的$array = array(2, 2, 3, 4);

3) Third cycle

php$value = $array[2]; // 由于$value引用$array[1],此语句相当于$array[1] = $array[2],也就是$array[1] = 3$value = &$array[2]; // $value现在变成了$array[2]的引用了

这时的$array = array(2, 3, 3, 4);

4) Fourth cycle

php$value = $array[3]; // 由于$value引用$array[2],此语句相当于$array[2] = $array[3],也就是$array[2] = 4$value = &$array[3]; // $value现在变成了$array[3]的引用了

这时的$array = array(2, 3, 4, 4);

Now let's take a look at why the script output 2233, the master can try to use the key ($array) inside the foreach to see what its return value is, you will find that the key () function returns a value of 1, that is, the pointer to the array always refers to the element that is labeled 1 down, So in each loop, the return value of the current ($array) function is always the value of the element with the subscript 1.

Finally, let's take a look at the value of the element with the array subscript 1 in each loop. Yes, these are the pictures below:

As for, why the key () function return value is always 1, the title can refer to the URL of the "Problem 2", there is a detailed explanation:)

Actually do not understand, because of curiosity, the study of the next.

$array = Array (1,2,3,4);
foreach ($array as $key = = $value) {
Echo current ($array);
}

Look at the results, you may be surprised, and I am surprised that the pointer has not changed. The answer is: 2 2 2 2

So current ($array) will only output the value of this array's subscript 1. The Output 2 2 3 3, as you now know, $value = & $array [$key]; This sentence changes the value of the original array.

$array = Array (1,2,3,4);
foreach ($array as $key = = $value) {
$value = & $array [$key];
Echo current ($array);
Var_dump ($array);
}

And then output look, the value of the array is always changing. The question comes again, so why does it change?

http://segmentfault.com/q/1010000002676308
Refer to this, this problem understand, you will naturally understand.

  • 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.