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?

Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • In your foreach loop shouldn't you refer to your array element using the $value variable? – Crackertastic Aug 10 '13 at 04:03
  • @Crackertastic, I'm not using any variables within the loop. – Pacerier Aug 10 '13 at 04:04
  • 1
    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)` – vee Aug 10 '13 at 04:07
  • @vinodadhikary, you ran the exact same code without modification? I'm on 5.3.26, what about you? – Pacerier Aug 10 '13 at 04:08
  • PHP 5.5.1, I get refcount=2. – Matthew Aug 10 '13 at 04:09
  • @Pacerier, Yes no code change. PHP version is 5.4.16. – vee Aug 10 '13 at 04:23
  • This is a due to a change in the implementation between PHP 5.3 and PHP 5.4. The most relevant code parts are http://lxr.php.net/xref/PHP_5_3/Zend/zend_vm_def.h#3663 (5.3) and http://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. – NikiC Aug 10 '13 at 19:25
  • @NikiC, btwt take a look at this too: http://stackoverflow.com/q/18173176/632951 – Pacerier Aug 11 '13 at 14:57

1 Answers1

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.

  • 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. – Pacerier Aug 10 '13 at 05:58
  • 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. –  Aug 10 '13 at 07:54
  • 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? – Pacerier Aug 10 '13 at 08:06
  • As I have said I don't have the xdebug thingy. I would report it as a bug to the creators of the plugin. –  Aug 10 '13 at 08:09
  • Thank you for the info but I don't need it as I use phpED as my IDE. I would still report this bug. –  Aug 10 '13 at 08:12
  • I don't think it's a bug. – Pacerier Aug 10 '13 at 08:18