PHP reference count and variable reference

Source: Internet
Author: User

Each php5.5 variable is stored in a variable container called Zval.

A Zval variable container that contains two bytes of extra information in addition to the type and value of the variable:

1, the first is "Is_ref", is a bool type, used to identify whether the variable belongs to the reference set (reference set), if it belongs to the value of 1, otherwise 0.

One of these variables is that the PHP engine can differentiate between ordinary variables and reference variables.

2, the second is "RefCount", used to indicate the number of points to this zval variable (symbol). Each symbol has scope (scope), and those main scripts and functions or methods also have scopes.

All symbols are present in a symbol table.

When a variable is assigned a constant value, a Zval variable container is generated, as in the following example:

<? PHP     $a = "Hello World";? >

This time execute the following program to get the $ A variable to point to the Is_ref and RefCount values in the Zval container

<? PHP     $a = "Hello World";     Print_r (Xdebug_debug_zval (' a ')); >
A: (refcount=1, is_ref=0) = ' Hello world '

Below, we perform the following experiments to explore the reference assignment and the normal assignment.

First, make $b point to $ A, and look at Is_ref, RefCount, as follows:

<? PHP     $a = "Hello World";       $b $a ;     Print_r (Xdebug_debug_zval (' a '));     Print_r (Xdebug_debug_zval (' B '));? >
A: (refcount=2, is_ref=0) ='HelloWorld'B: (RefCount=2 , is_ref=0) ='HelloWorld'

Let $b quote $ A To see Is_ref refcount, as follows

 <? php $a  =  hello World   "       = & $a; Print_r (Xdebug_debug_zval (  " a  Span style= "color: #800000;"    > '    " b  Span style= "color: #800000;" > '  ? 
A: (refcount=2, is_ref=1) ='HelloWorld'B: (RefCount=2 , is_ref=1) ='HelloWorld'

From the above we can analyze that when there are variables referencing the corresponding Zval container, the is_ref is 1.

We further analyze that we have $b quote $ A, $c point to $ A, as follows

<? PHP     " Hello World " ;       = &$a;     = $a;    Print_r (Xdebug_debug_zval ('a'));    Print_r (Xdebug_debug_zval ('b'));    Print_r (Xdebug_debug_zval ('C'));? >

Printing results are as follows

A: (refcount=2, is_ref=1) ='HelloWorld'B: (RefCount=2 , is_ref=1) ='HelloWorld'C: (refcount=1, is_ref= 0) ='HelloWorld'

Visible, this time the php5.5 engine has re-established a zval container for $c, the data type in the container, the value is exactly the same as the container where $ A is pointing, and the difference is the value of its refcount and is_ref.

Thus, we can see that the IS_REF variable in the Zval container of php5.5 either identifies the reference collection or identifies the normal collection, and when both are sometimes cloned zval container, to resolve the conflict.

  

 Summarize:

1. After php5.5, "variable assignment" is a pointer to an assignment, and a variable is directed to a specific Zval container.

2, "variable reference" is to bind variables and variables, if there is a variable in the binding variable to point to, then the other variables bound to each other to change the direction.

If the variable references the variable again, its original variable binding is lifted, and the new variable is bound instead. The following code:

  

<?phpfunction foo (& $var) {    $var =& $GLOBALS ["Baz"];} Foo ($bar);? >

  This causes foo the variables in the function to be $var bound together when the function is called $bar , but is then re-bound to the $GLOBALS["baz"] top. It is not possible to $bar bind to other variables within the scope of a function call by means of a reference mechanism, because there is no variable in the function foo $bar (it is represented as $var , but only the content of the $var variable is not called by the name-to-value binding in the symbol table). You can use a reference return to refer to the variable selected by the function.

PHP reference count and variable reference

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.