Questions about the PHP array reference

Source: Internet
Author: User
php$a =  array(1); $b =& $a[0];   //注释这条语句最后输出2,1$c = $a;$c[0]++;echo $c[0].$a[0]; // 输出 2,2   注释第二条语句,输出 2,1 。

Reply content:

php$a =  array(1); $b =& $a[0];   //注释这条语句最后输出2,1$c = $a;$c[0]++;echo $c[0].$a[0]; // 输出 2,2   注释第二条语句,输出 2,1 。

Note, however, that references inside arrays is potentially dangerous. Doing a normal (not by reference) assignment with a reference in the right side does not turn the left side into a referen CE, but references inside arrays is preserved in these normal assignments. This also applies to function calls where the array was passed by value.

In an array of assignments, if there is a reference to the data on the right, the corresponding element of the new array to which the value is assigned is also a reference, so changing the value of $c[0] will also change the value of $a[0].

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