PHP reference count and variable reference _php tutorial

Source: Internet
Author: User

PHP reference count and variable reference


Each php5.5 variable is stored in a variable container called Zval.       A Zval variable container, in addition to the type and value of the variable, contains two extra bytes of additional information: 1, the first is "Is_ref", is a bool type, to identify whether the variable is a 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: This time execute the following program to get the $ A variable to point to the Is_ref and RefCount values in the Zval container A: (refcount=1, is_ref=0) = ' Hello world ' below, we carry out 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: 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 A: (refcount=2, is_ref=1) = ' Hello World ' B: (refcount=2, is_ref=1) = ' Hello ' from the above we can analyze that when there are variables referencing the corresponding Zval container, Is_ref is 1. We further analyze that we have $b quote $ A, $c point to $ A, as follows
 
  

The results are printed 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 has re-established a zval container for $c, and the data type, value in the container is exactly the same as the one pointed to by $ A, unlike its refcount and is_ref values.      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.   Summary: 1, after the php5.5, "variable assignment" is to point to the assignment, will be a variable point 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:
 
  

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

http://www.bkjia.com/PHPjc/1053350.html www.bkjia.com true http://www.bkjia.com/PHPjc/1053350.html techarticle PHP reference counts and variable references each php5.5 variable is stored in a variable container called Zval. A Zval variable container that contains two bytes in addition to the type and value of the variable ...

  • Related Article

    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.