ID:               31118
 Updated by:       [EMAIL PROTECTED]
 Reported By:      dennis at inmarket dot lviv dot ua
-Status:           Open
+Status:           Verified
 Bug Type:         Zend Engine 2 problem
 Operating System: Win
 PHP Version:      5.0.2


Previous Comments:
------------------------------------------------------------------------

[2004-12-16 16:08:01] dennis at inmamrket dot lviv dot ua

Sorry for opening another ticket but this bug took me 2 weeks to
shuffle the code and I got really nervous about it. This is abnormal
behaviuor, and the bug you forwarded me to is different - there is
array modification inside the loop.

------------------------------------------------------------------------

[2004-12-16 16:05:33] dennis at inmarket dot lviv dot ua

No, it is NOT related no those bugs: here is a test case that proves
that with no array modification two consecutive foreach loops give
different results

$a = array('key1'=>'value1', 'key2'=>'value2', 'key3'=>'value3');
foreach($a as $k=>&$v) {
  echo $k . '=' . $v . "\r\n";
}
foreach($a as $k=>$v) {
  echo $k . '=' . $v . "\r\n";
}

This produces this:

key1=value1
key2=value2
key3=value3
key1=value1
key2=value2
key3=value2 <- must be 3

------------------------------------------------------------------------

[2004-12-16 12:43:59] [EMAIL PROTECTED]

Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

See #29992.

------------------------------------------------------------------------

[2004-12-16 12:26:02] dennis at inmarket dot lviv dot ua

Description:
------------
If you foreach an array with a reference (foreach $a as $x=>&$y),
modifying $y, and then without it (foreach $a as $x=>$y), on the second
time it will return incorrect results. 
The sample code has two functions - each iterates over array with a
reference to value and modifies it and then they foreach that array -
first function without a reference, and the second - with a reference.
The first function fails to iterate to the end of array. However, the
print_r shows the array is ok.

Reproduce code:
---------------
function test1() {
  $a = array('key1'=>'value1', 'key2'=>'value2', 'key3'=>'value3');
  foreach($a as $k=>&$v) {
    $v .= '0';
  }
  print_r($a);
  foreach($a as $k=>$v) {
    echo $k .'=' . $v . "\r\n";
  }
}

function test2() {
  $a = array('key1'=>'value1', 'key2'=>'value2', 'key3'=>'value3');
  foreach($a as $k=>&$v) {
    $v .= '0';
  }
  print_r($a);
  foreach($a as $k=>&$v) {
    echo $k .'=' . $v . "\r\n";
  }
}
test1();
test2();


Expected result:
----------------
Array
(
    [key1] => value10
    [key2] => value20
    [key3] => value30
)
key1=value10
key2=value20           <- all values properly changed
key3=value30     
Array
(
    [key1] => value10
    [key2] => value20
    [key3] => value30
)
key1=value10
key2=value20           <- all values properly changed
key3=value30

Actual result:
--------------
Array
(
    [key1] => value10
    [key2] => value20
    [key3] => value30
)
key1=value10
key2=value20
key3=value20           <- SHOULD BE '30'
Array
(
    [key1] => value10
    [key2] => value20
    [key3] => value30
)
key1=value10
key2=value20
key3=value30


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=31118&edit=1

Reply via email to