Bug #53405 [Opn->Asn]: accessing the iterator inside a foreach loop leads to strange results

From: Date: Tue, 15 Feb 2011 14:08:55 +0000
Subject: Bug #53405 [Opn->Asn]: accessing the iterator inside a foreach loop leads to strange results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-158789@lists.php.net to get a copy of this message
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)

« previous php.bugs (#158789) next »