Php reference count and variable reference. Php reference count and variable reference each php5.5 variable is stored in a variable container called zval. A zval variable container not only contains the type and value of the variable, but also contains two bytes of 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 bytes of additional information:
1. the first one is "is_ref", which is a bool type, used to identify whether the variable belongs to the reference set. if it belongs to the variable, its value is 1; otherwise, it 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, as shown in the following example:
$ A = "Hello world ";
?>
Run the following program to obtain the value of $ a pointing to is_ref and refcount in the zval container.
$ A = "Hello world ";
Print_r (xdebug_debug_zval ('A '));
?>
A: (refcount = 1, is_ref = 0) = 'Hello world'
Next, we will conduct the following experiments to explore the quote assignment and normal assignment.
First, point $ B to $ a to view is_ref and refcount, as shown below:
$ A = "Hello world ";
$ B = $;
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 $ a and view is_ref refcount, as shown below:
$ A = "Hello world ";
$ B = & $;
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 analysis, 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:
The output is 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'
It can be seen that 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 $, 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.
Summary:
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:
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.
Every 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 bytes...