The question of the & value reference in PHP, the result of the Foreach Loop can help explain what the output principle is? C # foreach JavaScript foreach c foreach

Source: Internet
Author: User



foreach ($arr as & $value) {echo ' value: '. $value. '

foreach ($arr as $value) {echo ' value: '. $value. '

?>
Output Result:
Value:one




Value:two


The first foreach with & does not change the contents of the array:
Instead of the last loop, $value references the last item in the array (you can test that the result of the second loop will not change after the first loop is unset ($value).
In your second foreach is also used by the $value variable, which creates a weird problem (you can change a variable, such as $val, the output array will not change).
The second foreach is assigned to $value, but at this point the $value is the last value of the referenced array,
So
The first loop assigns the one value to the last value,
The second assigns two to the last one,
The third and last one has been assigned to two in the second loop, so it is also two.

The above describes the PHP & value reference problem, the result of the Foreach Loop can help explain the output of the principle of what? , including the foreach aspect of the content, want to be interested in PHP tutorial friends helpful.

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