PHP foreach Reference &

Source: Internet
Author: User
Tags php foreach

Print the following code

$variable = [‘a‘, ‘b‘, ‘c‘];foreach ($variable as $key => &$value) {}foreach ($variable as $key => $value) {}echo "<pre />";var_dump($variable);echo "<pre />";#结果array(3) {  [0]=>  string(1) "a"  [1]=>  string(1) "b"  [2]=>  &string(1) "b"}

The printed result is inconsistent with the expected result because the reference is used in the first foreach, and the $value becomes a reference variable, so $value and $a [2] point to the same address space (shared variable value), in the second foreach, The value of the $arr is constantly assigned to $value, and the value of $arr[2] will be changed by the $value of the follower , and the result of the $variable printed in the second time

Changes in $value value and $arr[2] Value

PHP foreach 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.