php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50223 foreach() affects array pointer unexpectedly (different between PHP versions)
Submitted: 2009-11-18 23:44 UTC Modified: 2009-11-20 11:39 UTC
From: disposable_address at kennel17 dot co dot uk Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.2.11 OS: Windows
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-11-18 23:44 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-19 11:14 UTC] colder@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-20 11:39 UTC] disposable_address at kennel17 dot co dot uk
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?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 06:01:35 2024 UTC