PHP reference count and variable reference

Source: Internet
Author: User
Tags bind count variables reference variable
 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", and is a bool that identifies whether the variable belongs to a reference set (reference set). If it is a value of 1, otherwise 0.   This variable PHP engine is able to distinguish between ordinary variables and reference variables.   2, and the second is "RefCount", which indicates the number of points that point to the zval variable (symbol). Each symbol has scope (scope), and those main scripts and functions or methods are scoped.   All symbols exist 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 $a variable pointing zv Is_ref and RefCount values in al containers   <?php     $a = "Hello world";     Print_r (Xdebug_debug_zval (' a '));?> A: (refcount=1, is_ref=0) = ' Hello world ', we conducted the following experiment to explore reference assignment and normal assignment.   First, make $b point to $a, view 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) = ' Hello world ' B: (refcount=2, is_ref=0) = ' Hello World ' let $b quote $a, view is_ref    refcount, as follows   <?php &NBsp   $a = "Hello world";       $b = & $a;     Print_r (Xdebug_debug_zval (' a '));     Print_r (xdebug_debug_zval (' B '));?> A: (refcount=2, is_ref=1) = ' Hello world ' B: (refcount=2, is_ref=1) = ' Hello World ' from the above we can analyze that when a variable refers to the corresponding Zval container, the is_ref is 1.   We further analyze, we refer to $b reference $a, $c point to $a, as follows    
<?php
    $a = "Hello world";  
    $b = & $a;
    $c = $a;

    Print_r (Xdebug_debug_zval (' a '));
    Print_r (Xdebug_debug_zval (' B '));
    Print_r (Xdebug_debug_zval (' C '));
? >
Print the result as follows a: (refcount=2, is_ref=1) = ' Hello World ' B: (refcount=2, is_ref=1) = ' Hello World ' C: (refcount=1, is_ref=0) = ' Hello        World ' visible, this time the php5.5 engine $c a zval container in which the data type, the value is exactly the same as in the container that $a points to, and the RefCount and Is_ref values are different.          As a result, we can see that the IS_REF variable in the php5.5 zval container either identifies the reference set or identifies the common collection, and when both are sometimes, he clones the Zval container to resolve the conflict problem.     Summary: 1, after php5.5, "variable assignment" is to point to the assignment, the variable will point to a specific zval container.       2, "variable reference" is to bind the variable with the variable, if the bound variable has a variable to point to, then the other bound to the point of the other variables along with the change. If a variable references a variable, its original variable binding is lifted, and the new variable is bound instead. The following code:
<?php
function foo (& $var)
{
    $var =& $GLOBALS ["Baz"];
}
Foo ($bar);
? >
This will cause the $var variable in the Foo function to be bound to the $bar at the time of the function call, but then to be rebind to the $GLOBALS ["Baz"]. It is not possible to bind a $bar to another variable in the scope of a function call through a reference mechanism, because there is no variable $bar in the function foo (it is represented as $var, but $var only the variable content but not the binding of the name to the value in the symbol table). You can use reference returns to refer to variables that are selected by the function.

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.