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

From: Date: Mon, 09 Mar 2015 11:16:17 +0000
Subject: Bug #53405 [Asn->Csd]: 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-191265@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=53405&edit=1 ID: 53405 Updated by: nikic@php.net Reported by: jpauli@php.net Summary: accessing the iterator inside a foreach loop leads to strange results -Status: Assigned +Status: Closed Type: Bug Package: Scripting Engine problem Operating System: *nix PHP Version: 5.3.3 Assigned To: nikic Block user comment: N Private report: N New Comment: This was fixed by https://wiki.php.net/rfc/php7_foreach, the output is now as expected (three a's). Previous Comments: ------------------------------------------------------------------------ [2013-04-09 20:13:16] nikic@php.net Here is an initial patch: https://github.com/nikic/php-src/commit/185c7fae3a3d7a90693c24726a8f2685b3751464 Not yet quite done though. ------------------------------------------------------------------------ [2013-04-09 18:05:02] 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 14:28:36] 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 10:10:25] 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-08 23:39:38] 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. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=53405 -- Edit this bug report at https://bugs.php.net/bug.php?id=53405&edit=1

« previous php.bugs (#191265) next »