In PHP, how does the result of the foreach loop help explain the principle of the output result ?, PHP_PHP tutorial

Source: Internet
Author: User
The & amp; pass-through reference problem in PHP. what is the principle of the output result after the result of the foreach loop ?, PHP. In PHP, the result of the foreach loop can help explain the principle of the output result ?, In PHPPHP, the result of the foreach loop can help to explain the reference of the value transfer in PHP. what is the principle of the output result after the result of the foreach loop ?, PHP
In PHP, how does the result of the foreach loop help explain the principle of output results?
The code is as follows:
$ Arr = array ('one', 'two', 'Three ');
Foreach ($ arr as & $ value) {echo 'value: '. $ Value .'
';}
Foreach ($ arr as $ value) {echo 'value: '. $ Value .'
';}
?>
Output result:
Value: one
Value: two
Value: three

Value: one
Value: two
Value: two

The foreach with & for the first time does not change the content of the array ..
Instead, the last loop $ value references the last item of the array (you can test whether the result of the second loop will not change after the first loop ends unset ($ value ),
In your second foreach, $ value variable is also used, which causes a weird problem (you can change the variable, such as $ val, and the output array will not change ).
The second foreach is assigned to $ value, but $ value is the last value of the referenced array,
So
In the first loop, one is assigned to the last value,
The second assignment of two to the last one,
The third time, that is, the last one has been assigned as two in the second loop, so it is still two.

Why ?, The result of the foreach loop can help explain the problem of passing value reference in PHP...

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.