NikiC stated in another thread:
Right before [a foreach] iteration the
$array
is "soft copied" for use in foreach. This means that no actual copy is done, but only the refcount of the zval of$array
is increased to 2.
However, my test code is showing a different result:
$array = array(0, 1, 2);
xdebug_debug_zval('array'); // refcount=1, is_ref=0
// so far so good
foreach ($array as $key => $value) {
xdebug_debug_zval('array'); // refcount=3, is_ref=0
} // why is refcount 3 instead of 2?
Just by looking at the code, we can see at most two array variables.
Why is refcount 3
?
Why isn't refcount 2
after foreach
is run?
refcount
2, am I missing something?array: (refcount=2, is_ref=0)=array (0 => (refcount=1, is_ref=0)=0, 1 => (refcount=1, is_ref=0)=1, 2 => (refcount=2, is_ref=0)=2)
– Bosporus