Php reference count and variable reference

Source: Internet
Author: User

Php reference count and variable reference
Each php5.5 variable is stored in a variable container named zval. A zval variable container not only contains the type and value of the variable, but also contains two additional Bytes: 1. The first is "is_ref" and is a bool type, used to identify whether the variable belongs to the reference set. If it belongs to the set, the value is 1; otherwise, the value is 0. The php engine can separate common variables from referenced variables. 2. The second is "refcount", which indicates the number of zval variables (symbols. Each symbol has a scope, and all the main scripts and functions or methods have a scope. All symbols exist in a symbol table. When a variable is assigned a constant value, a zval variable container is generated, for example, <? Php $ a = "Hello world";?> Run the following program to get the value of $ a pointing to is_ref and refcount in the zval container. <? Php $ a = "Hello world"; print_r (xdebug_debug_zval ('A');?> A: (refcount = 1, is_ref = 0) = 'Hello world'. Below, we will conduct the following experiment to explore the quote assignment and normal assignment. First, point $ B to $ a and view is_ref and 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 Reference $, view is_ref refcount, as shown below <? Php $ 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' can be analyzed from the above, when a variable references the corresponding zval container, is_ref is 1. For further analysis, we reference $ a and $ c to $ a, as shown below:

<?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'));?>

 

The output is as follows: (refcount = 2, is_ref = 1) = 'Hello world' B: (refcount = 2, is_ref = 1) = 'Hello world' c: (refcount = 1, is_ref = 0) = 'Hello world' visible. At this time, the php5.5 engine re-creates a zval container for $ c, the data type and value in the container are exactly the same as those in the container pointed to by $ a. The difference is the refcount and is_ref values. Therefore, we can see that the is_ref variable in the zval container of php5.5 either identifies the referenced set or the common set. when both are present, it will clone the zval container to solve the conflict problem. Conclusion: 1. After php5.5, "variable assignment" points to assignment, that is, to point a variable to a specific zval container. 2. "variable reference" binds a variable to the variable. If one of the bound variables changes its direction, the other variables that are bound to each other also change. If the variable references the variable again, the original variable is unbound and then bound to the new variable. The following code:
<?phpfunction foo(&$var){    $var =& $GLOBALS["baz"];}foo($bar);?>

 

This binds the $ var variable in the foo function to $ bar when calling the function, but is then re-bound to $ GLOBALS ["baz. It is impossible to bind $ bar to another variable within the function call range through the reference mechanism, because there is no variable $ bar in function foo (it is represented as $ var, but $ var only contains the variable content and does not call the name-to-value binding in the symbol table ). You can use the reference return to reference the variable 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.