With reference to this situation who can explain the next

Source: Internet
Author: User
Code Snippet 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 A: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)

Here the variable D value is not changed, well understood, but change the reference position of the variable C, as follows:
Code Snippet 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 A: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 also became a reference, do not understand why
Ask the Great God for advice


Reply to discussion (solution)

The manual says: If an array with a reference is copied, its value is not dereferenced. This is true for arrays that pass values to functions.

$array _c = & $array _b[0]; This allows $array_b to have a reference, C and b[0] pointing to the same memory

above example:

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