$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, is_ref is 1.
We analyze further, we refer $b to $a, $c points 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'));
?>
Copy after login
The print results are 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-establishes 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 value of its refcount and is_ref.
Therefore, we can see that the is_ref variable in the zval container of php5.5 either identifies a reference collection or a normal collection. When both are present, it will clone the zval container to resolve the conflict problem.
Summary:
1. After php5.5, "variable assignment" refers to point assignment, that is, a variable points to a specific zval container.
2. "Variable reference" binds variables to variables. If one of the bound variables changes its direction, the directions of other variables bound to each other will also change.
If a variable re-references a variable, its original variable binding is released and the new variable is bound instead. The following code:
<?php
function foo(&$var)
{
$var =& $GLOBALS["baz"];
}
foo($bar);
?>
Copy after login
This will cause the $var variable in the foo function to be bound to $bar when the function is called, but then re-bound to $GLOBALS["baz"]. It is not possible to bind $bar to another variable within the function call scope through the reference mechanism, because there is no variable $bar in function foo (it is represented as $var, but $var only has the variable content and no call symbol table name-to-value binding). You can use reference returns to reference variables selected by the function.
http://www.bkjia.com/PHPjc/1053350.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1053350.htmlTechArticlephp reference counting 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, also contains two bytes...