PHP Address Reference

Source: Internet
Author: User
There is a difference between PHP's address reference and the pointer in the C language. The pointer inside the C language stores the address where the contents of the variable are stored in memory.
Talk less, see examples first,

$a = "ABC";
$b =& $a;
echo $a; Output here: ABC
Echo $b; Output here: ABC
$b = "EFG";
echo $a; Here a $ A value becomes EFG so the output EFG
Echo $b; Output EFG Here
?>

In the above example, we all understand that we will not say more. So take a look at the following example?

$a = "ABC";
$b =& $a;
unset ($a);
Echo $b; Output here: ABC
?>

It turns out this way, when you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed.

Here's a little episode.
PHP in the direction of the address (similar to the pointer) function is not implemented by the user itself, is implemented by the Zend Core, PHP refers to the use of "copy-on-write" principle, that is, unless a write operation, the same address to the variable or object is not copied.
In layman's words, if you have the following code

$a = "ABC";
$b = $a;
?>

In fact, $a and $b both point to the same memory address and not $ A and $b occupy different memory
2: If you add the following code based on the above code

$a = "EFG";

Since $ A and $b point to the memory of the data to be re-written once, at this time Zend core will automatically decide to automatically produce a $ A copy of the data for $b, re-request a piece of memory for storage

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