Example #1 generate a new Zval container
$a = "new string";
?>
In the example above, the new variable A is generated in the current scope. and a variable container of type string and a value of new string is generated. In the additional two bytes of information, "Is_ref" is set to FALSEby default because there are no custom references generated. "RefCount" is set to 1 because there is only one variable that uses this variable container. Notice that when the value of "RefCount" is 1 o'clock, the value of "Is_ref" is always FALSE. If you have installed»xdebug, you can display the values of "RefCount" and "Is_ref" by calling the function xdebug_debug_zval () .
Example #2 show zval information
Xdebug_debug_zval (' a ');
?>
The above routines will output:
A: (refcount=1, is_ref=0) = ' new String '
Assigning a variable to another variable increases the number of references (refcount).
Example #3 Add a zval reference count
$a = "new string";
$b = $a;
Xdebug_debug_zval (' a ');
?>
The above routines will output:
A: (refcount=2, is_ref=0) = ' new String '
At this point, the number of references is 2 because the same variable container is associated with variable A and variable B. When not necessary, PHP does not replicate the generated variable container. The variable container is destroyed when "RefCount" becomes 0 o'clock. When any variable associated to a variable container leaves its scope (for example, the function execution ends), or the function unset () is called on the variable, "RefCount" is reduced by 1, and the following example illustrates:
Example #4 Reduce reference count
$a = "new string";
$c = $b = $a;
Xdebug_debug_zval (' a ');
Unset ($b, $c);
Xdebug_debug_zval (' a ');
?>
The above routines will output:
A: (refcount=3, is_ref=0) = ' new String ' A: (Refcount=1, is_ref=0) = ' new String '
If we now execute unset ($a), the variable container containing the type and value is removed from memory.