Bug #53405 [NEW]: accessing the iterator inside a foreach loop leads to strange results

From: Date: Thu, 25 Nov 2010 17:38:15 +0000
Subject: Bug #53405 [NEW]: accessing the iterator inside a foreach loop leads to strange results
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-156447@lists.php.net to get a copy of this message
From:             
Operating system: *nix
PHP version:      5.3.3
Package:          Scripting Engine problem
Bug Type:         Bug
Bug description:accessing the iterator inside a foreach loop leads to strange results

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 bug report at http://bugs.php.net/bug.php?id=53405&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=53405&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=53405&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=53405&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=53405&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53405&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=53405&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=53405&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=53405&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=53405&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=53405&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=53405&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=53405&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=53405&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=53405&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=53405&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=53405&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=53405&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=53405&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=53405&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=53405&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=53405&r=mysqlcfg



Thread (8 messages)

« previous php.bugs (#156447) next »