Which expert can explain the reference in this case?

Source: Internet
Author: User
For reference, who can explain the following code snippet 1: $ array_a = & nbsp; array (& nbsp; 0 & nbsp ;=& gt; & nbsp; 'A' & nbsp;); $ array_ B = & nbsp; & amp; $ array_a; $ array_c = & nbsp; who can explain this situation?
Code Segment 1:

$ Array_a = array (0 => 'A ');
$ Array_ B = & $ array_a;
$ Array_c = & $ array_ B;
$ Array_d = $ array_ B;

$ Array_c [0] = 'B ';

Echo 'variable a: '; var_dump ($ array_a );
Echo 'variable B: '; var_dump ($ array_ B );
Echo 'variable c: '; var_dump ($ array_c );
Echo 'variable d: '; var_dump ($ array_d );

Output result:

Variable:
Array (size = 1)
0 => string 'B' (length = 1)
Variable B:
Array (size = 1)
0 => string 'B' (length = 1)
Variable c:
Array (size = 1)
0 => string 'B' (length = 1)
Variable d:
Array (size = 1)
0 => string 'A' (length = 1)

The variable D value is not changed, which is easy to understand, but the reference position of variable c is changed as follows:
Code Segment 2:

$ Array_a = array (0 => 'A ');
$ Array_ B = & $ array_a;
$ Array_c = & $ array_ B [0];
$ Array_d = $ array_ B;

$ Array_c = 'B ';

Echo 'variable a: '; var_dump ($ array_a );
Echo 'variable B: '; var_dump ($ array_ B );
Echo 'variable c: '; var_dump ($ array_c );
Echo 'variable d: '; var_dump ($ array_d );

Output result:

Variable:
Array (size = 1)
0 => & string 'B' (length = 1)
Variable B:
Array (size = 1)
0 => & string 'B' (length = 1)
Variable c:
String 'B' (length = 1)
Variable d:
Array (size = 1)
0 => & string 'B' (length = 1)

Here, the variable d also changed and changed to a reference. I don't understand why.
Please advise
Reference shared :? 'A '?); $ Array_ B =? & $ Array_a; $ array_c =? & $ Array_ B; $ array_d... 'data-pics = ''>
------ Solution --------------------
As mentioned in the manual, if an array with reference is copied, its value will not be removed from the reference. This is also true for passing values from arrays to functions.

$ Array_c = & $ array_ B [0]; // so that $ array_ B has a reference, and c and B [0] point to the same memory

Example above:


/* Common variable */
$ A = 1;
$ B = & $;
$ C = $ B;
$ C = 7; // The change of $ c does not affect $ a or $ B, and $ a and $ B still point to 1.

/* Array */
$ Arr = array (1 );
$ A = & $ arr [0]; // $ a and $ arr [0] point to the same
$ Arr2 = $ arr; // no & value assigned
$ Arr2 [0] ++;
/* $ A = 2, $ arr = array (2 )*/
/* Even if it is not a reference value, it also changes */
?>

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.