ID:               50223
 User updated by:  disposable_address at kennel17 dot co dot uk
 Reported By:      disposable_address at kennel17 dot co dot uk
 Status:           Wont fix
 Bug Type:         Scripting Engine problem
 Operating System: Windows
 PHP Version:      5.2.11
 New Comment:

If this is a known problem, why is it a wontfix?  Surely bugs should be
fixed - that's how things get better!

Calling reset() within a foreach loop (as suggested in that notice) is
not possible, because then it will be reset on every iteration.  The
only (completely un-intuitive) solution is therefore to have an extra
variable to track whether this is the first iteration of the loop, and
only reset() it in that instance.

However, it is not clear whether even this method is 'safe'.

Perhaps there should be a big bold notice on the next()/current()/etc.
pages that says DO NOT USE THESE FUNCTIONS INSIDE ANY LOOPING CONSTRUCT,
AS THEY DO NOT WORK.

Perhaps you should also follow that up with "THIS IS NOT BY DESIGN, BUT
NONETHELESS WE DON'T PLAN ON FIXING THE PROBLEM", just to avoid
confusion.

Or even better, fix it, and note the versions of PHP where it doesn't
work as expected.  Maybe in PHP6?  After all, you are trying to improve
the language, aren't you?


Previous Comments:
------------------------------------------------------------------------

[2009-11-19 11:14:27] col...@php.net

This is a known "problem":

php.net/foreach : "foreach  has some side effects on the array pointer.
Don't rely on the array pointer during or after the foreach without
resetting it." 

------------------------------------------------------------------------

[2009-11-18 23:44:27] disposable_address at kennel17 dot co dot uk

Description:
------------
The array pointer does not behave in the expected manner within a
foreach() loop.  What's more, the behaviour differs between PHP4 and
PHP5.



Reproduce code:
---------------
$arr = array(1, 2, 3);

end($arr);

var_dump(current($arr));
foreach ($arr as $Item) {
print("Item " . $Item . " - ");
var_dump(current($arr));
}

var_dump(current($arr));


Expected result:
----------------
EITHER the array pointer IS NOT affected by the loop:

  int(3)
  Item 1 - int(3)
  Item 2 - int(3)
  Item 3 - int(3)
  int(3)

OR the array point IS affected by the loop:

  int(3)
  Item 1 - int(1)
  Item 2 - int(2)
  Item 3 - int(3)
  int(2)

I am not sure which is best, but it seems to me that any other
behaviour is incorrect.

Actual result:
--------------
In PHP4 (4.4.9):

  int(3)
  Item 1 - int(1)
  Item 2 - int(1)
  Item 3 - int(1)
  int(1)

In PHP5 (5.2.10)

  int(3)
  Item 1 - int(2)
  Item 2 - int(2)
  Item 3 - int(2)
  int(2)

Both of these seem wrong, but the fact that they are different seems
doubly wrong!  Calling reset() before the foreach() loop doesn't make
any difference.  The PHP4 version seems more acceptable (it sets it to
the first element on entry, and then doesn't affect it at all), however
the PHP5 behaviour just seems absurd.

Note: The described behaviour is the same on Windows Vista and Windows
XP.


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=50223&edit=1

Reply via email to