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

 ID:                 53405
 Updated by:         dmi...@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:        dmitry
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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