PHP address reference

Source: Internet
Author: User

The PHP address reference is different from the pointer in the C language. The pointer in the C language stores the address where the variable content is stored in the memory.
Let's talk nonsense. Let's look at the example first,

<? PHP
$ = " ABC " ;
$ B   = & $ ;
Echo   $ ; // Output here: ABC
Echo   $ B ; // Output here: ABC
$ B = " EFG " ;
Echo   $ ; // Here, the value of $ A is changed to EFG, so EFG is output.
Echo   $ B ; // EFG output here
?>  

 

As you can see in the above example, I will not talk about it any more. Let's take a look at the following example?

<? PHP
$   =   " ABC " ;
$ B   = &   $ ;
Unset ( $ );
Echo   $ B ; // Output here: ABC
?>  

 

It turns out this way. When you unset a reference, you just disconnect the binding between the variable name and the variable content. This does not mean that the variable content is destroyed.
 
// Next is an episode
In PHP, the address pointing (similar to pointer) function is not implemented by the user, but is implemented by the Zend core. in PHP, the reference uses the principle of "Copy at write, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.
In general, if the followingCode

<? PHP
$ = " ABC " ;
$ B = $ ;
?>

 

In fact, both $ A and $ B point to the same memory address, not $ A and $ B occupy different memory.
2: Add the following code on the basis of the above Code:

$ = " EFG " ;

 

Because the memory data pointed to by $ A and $ B needs to be re-written, the Zend core automatically determines that $ B will generate a $ A data copy, apply for a new 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.