Home > Article > Backend Development > PHP garbage collection mechanism reference counter concept analysis_PHP tutorial
If you have xdebug installed, you can use xdebug_debug_zval() to display "zval" information. As follows:
Result:
str:
(refcount=1, is_ref=0),
string 'jb51.net' (length=10)
Only when the variable container is destroyed when "refcount" becomes 0. When you unset() a variable, the refcount in the desired "zval" will be reduced by 1. Let's talk about what we encountered a few days ago. unset reference problem:
Result:
b:
(refcount=1, is_ref=0), string 'aaa' (length=3)
Continuing to talk about the reference counter issue, the situation is different for array and object types:
Result:
arr:
(refcount=1, is_ref=0),
array
'a' => (refcount=1, is_ref=0), string 'aaa' (length=3)
'b' => (refcount=1, is_ref=0),string 'bbb' (length=3)
arr:
(refcount=1, is_ref=0),
array
'a' => (refcount=2, is_ref=0),string 'aaa' (length=3)
'b' => (refcount=1, is_ref=0),string 'bbb ' (length=3)
'aaa' => (refcount=2, is_ref=0),string 'aaa' (length=3)
You can see that the original array elements and the newly added array elements are associated with the same zval variable container of "refcount" 2. Here I am just trying to inspire others.
For details about PHP reference counters, please refer to the manual: http://php.net/manual/zh/features.gc.refcounting-basics.php