php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53405 accessing the iterator inside a foreach loop leads to strange results
Submitted: 2010-11-25 18:38 UTC Modified: 2015-03-09 11:16 UTC
Votes:4
Avg. Score:4.2 ± 0.8
Reproduced:3 of 4 (75.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: jpauli@php.net Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.3.3 OS: *nix
Private report: No CVE-ID: None
 [2010-11-25 18:38 UTC] 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

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-15 15:08 UTC] dmitry@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: dmitry
 [2013-04-08 23:39 UTC] chris at clowerweb dot com
I actually experienced something last night during Wordpress development that I 
believe to be related to this.

<ul>
<?php
$posts = get_posts();

foreach($posts as $post) {
  setup_postdata($post); ?>
  <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } ?>
</ul>

Somehow the foreach loop was manipulating a $post (not $posts) variable in 
another 
part of the script, causing post pages to display incorrect content.
 [2013-04-09 10:10 UTC] dmitry@php.net
First off all mixing foreach() and reset/next/current is not a good idea.
In general the value of internal iterator pointer is undefined when using foreach().

At second "false" is more expected value, than "a".

The value "b" may be explained as well. The current() call in the foreach loop separates the value of variable $a from the array used by foreach loop. So we got two copies of arrays with different iterator positions.

I don't think we should try to fix it. It's better to add the note into documentation about undefined behaviour in case of mixing foreach and reset/next/current.
 [2013-04-09 14:28 UTC] jpauli@php.net
-Status: Assigned +Status: Not a bug
 [2013-04-09 14:28 UTC] jpauli@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Ok, I'm gonna add a doc hint.
 [2013-04-09 18:05 UTC] nikic@php.net
Taking over this bug. I have an alternative foreach implementation that does not rely on the IAP and as such fixes this (and other) problems. I'll try to clean it up sometime soon.
 [2013-04-09 18:05 UTC] nikic@php.net
-Status: Not a bug +Status: Assigned -Assigned To: dmitry +Assigned To: nikic
 [2013-04-09 20:13 UTC] nikic@php.net
Here is an initial patch: https://github.com/nikic/php-src/commit/185c7fae3a3d7a90693c24726a8f2685b3751464

Not yet quite done though.
 [2015-03-09 11:16 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2015-03-09 11:16 UTC] nikic@php.net
This was fixed by https://wiki.php.net/rfc/php7_foreach, the output is now as expected (three a's).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC