Bug #53405 [Opn->Asn]: accessing the iterator inside a foreach loop leads to strange results
Edit report at http://bugs.php.net/bug.php?id=53405&edit=1
ID: 53405
Updated by: dmitry@php.net
Reported by: jpauli@php.net
Summary: accessing the iterator inside a foreach loop leads
to strange results
-Status: Open
+Status: Assigned
Type: Bug
Package: Scripting Engine problem
Operating System: *nix
PHP Version: 5.3.3
-Assigned To:
+Assigned To: dmitry
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2010-11-25 18:38:12] jpauli@php.net
Description:
------------
foreach() is supposed to work on a copy of the iternal iterator.
However, manipulating the iterator inside the foreach loop leads to very
strange results.
-> Also try to print the result of current() inside the foreach loop in
the 3 use cases provided. You'll see that the iterator is some kind of
manipulated by foreach
Test script:
---------------
$a = range('a','d');
foreach ($a as $v) { }
var_dump(current($a));
$a = range('a','d');
foreach ($a as $v) { current($a); }
var_dump(current($a));
$a = range('a','d');
foreach ($a as &$v) { current($a); }
var_dump(current($a));
Expected result:
----------------
'a'
'a'
'a'
Actual result:
--------------
false
'b'
false
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=53405&edit=1
Thread (8 messages)