$var = 1;
debug_zval_dump($var);
Output:
long(1) refcount(2)
$var = 1;
$var_dup = &$var;
debug_zval_dump($var);exit;
Output :
long(1) refcount(1)
UPDATE
Very disapointed at the answer...
Code:
$var = 1;
debug_zval_dump($var);
Output: long(1) refcount(2)
Explanation: When a variable has a single reference, as did $var before it was used as an argument to debug_zval_dump(), PHP's engine optimizes the manner in which it is passed to a function. PHP, basically makes a pointer to the variable and internally, PHP treats $var like a reference and so it's refcount is increased for the scope of this function.
Code:
$var = 1;
$var_dup = &$var;
debug_zval_dump($var);exit;
Output: long(1) refcount(1)
Explanation: Here the $var variable is copyied on write, making whole new seprate instance of that varable and because debug_zval_dump is dealing with a whole new copy of $var, not a reference, it's refcount is 1. The copy is then destroyed once the function is done.
Hope that clears it up.
I think the documentation for this method explains this under the section "Beware the Ref Count":
debug_zval_dump
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With