php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33713 Foreach uses the same internal pointer in different iterations of recursive fns
Submitted: 2005-07-15 18:41 UTC Modified: 2005-07-15 22:01 UTC
From: bmorel at ssi dot fr Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
 [2005-07-15 18:41 UTC] bmorel at ssi dot fr
Description:
------------
Sorry if this is not a bug, but I think the behavior is not normal, even after searching the net and this bug database.

It seems that, used inside a recursive function, foreach doesn't use different array pointers for each iteration of the function, but uses the same at each time. This common pointer is reseted at each foreach() call.

Reproduce code:
---------------
function recurse($doRecurse = true) {
 static $array = array(1,2);
 foreach ($array as $value) {
  echo $value . PHP_EOL;
  if ($doRecurse) recurse(false);
 }
}
recurse();

Expected result:
----------------
1
1
2
2
1
2

Actual result:
--------------
1
1
2

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-15 22:01 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Use ArrayObject/ArrayIterator instead or as a wrapper around your arrays.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 07 23:01:32 2025 UTC