Learning about the PHP garbage collection mechanism, understanding the concept of referencing counters. a php variable exists in a variable container named "zval". The "zval" variable container includes the types and values containing variables, it also includes two additional bytes of information, "is_ref" indicating whether the variable is referenced, and "refcount" indicates the number of variables in the zval variable container.
If you have installed xdebug, you can use xdebug_debug_zval () to display "zval" information. As follows:
Result:
Str:
(Refcount = 1, is_ref = 0 ),
String 'phpddt. com' (length = 10)
The variable container is destroyed only when "refcount" is changed to 0. when you unset () a variable, the refcount in the desired "zval" will be reduced by 1. let's talk about the unset reference problem encountered a few days ago:
Result:
B:
(Refcount = 1, is_ref = 0), string 'AAA' (length = 3)
Continue to talk about the reference counter problem. The matching types of array and object are different:
'aaa', 'b' => "bbb" );xdebug_debug_zval( 'arr' );$arr['aaa'] = $arr['a'];xdebug_debug_zval( 'arr' );?>
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)
We can see that the original array elements and newly added array elements are associated with the zval variable container of the same "refcount" 2. here I am only playing a role in attracting others.
For details about the PHP reference counter can refer to the manual: http://php.net/manual/zh/features.gc.refcounting-basics.php
Reprinted! Original address: http://www.phpddt.com/php/gc-refcounting-basics.html, reproduced please indicate the address, thank you!