Edit report at https://bugs.php.net/bug.php?id=53405&edit=1

 ID:                 53405
 Comment by:         ni...@php.net
 Reported by:        jpa...@php.net
 Summary:            accessing the iterator inside a foreach loop leads
                     to strange results
 Status:             Assigned
 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:

Here is an initial patch: 
https://github.com/nikic/php-src/commit/185c7fae3a3d7a90693c24726a8f2685b3751464

Not yet quite done though.


Previous Comments:
------------------------------------------------------------------------
[2013-04-09 18:05:02] ni...@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] jpa...@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] dmi...@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.

------------------------------------------------------------------------
[2010-11-25 18:38:12] jpa...@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 https://bugs.php.net/bug.php?id=53405&edit=1

Reply via email to