Why does foreach increase refcount by 2 instead of 1?
Asked Answered
H

1

6

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?

Haileyhailfellowwellmet answered 10/8, 2013 at 3:55 Comment(8)
In your foreach loop shouldn't you refer to your array element using the $value variable?Knockwurst
@Crackertastic, I'm not using any variables within the loop.Haileyhailfellowwellmet
I'm getting 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
@vinodadhikary, you ran the exact same code without modification? I'm on 5.3.26, what about you?Haileyhailfellowwellmet
PHP 5.5.1, I get refcount=2.Flock
@Pacerier, Yes no code change. PHP version is 5.4.16.Bosporus
This is a due to a change in the implementation between PHP 5.3 and PHP 5.4. The most relevant code parts are lxr.php.net/xref/PHP_5_3/Zend/zend_vm_def.h#3663 (5.3) and lxr.php.net/xref/PHP_5_5/Zend/zend_vm_def.h#4187 (5.5). I don't know off the top of my head why the code previously used a PZVAL_LOCK there.Nightshade
@NikiC, btwt take a look at this too: https://mcmap.net/q/376636/-why-does-foreach-copy-the-array-when-we-did-not-modify-it-in-the-loop-duplicate/632951Haileyhailfellowwellmet
A
1

The xdebug_debug_zval() is looking at the $array variable and not the $key variable. if you change your code to:

foreach ($array as $key => $value) {
    echo $key . " : " . $values . "<br>";
    //xdebug_debug_zval('array');

}

The correct values of the array will be returned. I don't have the xdebug function so I can't test what value you put in there.

Almazan answered 10/8, 2013 at 4:46 Comment(6)
I've tested your code and xdebug_debug_zval('array'); still shows 3 instead of 2. I'm not testing the values of the array, but the refcount of the array.Haileyhailfellowwellmet
OK. While in the foreach loop you will need to change the xdebug... value. At the moment you are always looking at the entire array not the item in the array.Almazan
I don't want to look at the item in the array.... I'm looking at the array because that is what this question is about. Why does the array give refcount 3 instead of 2?Haileyhailfellowwellmet
As I have said I don't have the xdebug thingy. I would report it as a bug to the creators of the plugin.Almazan
Thank you for the info but I don't need it as I use phpED as my IDE. I would still report this bug.Almazan
I don't think it's a bug.Haileyhailfellowwellmet

© 2022 - 2024 — McMap. All rights reserved.