|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62901 foreach unexpectedly advances the internal array pointer
Submitted: 2012-08-23 02:08 UTC Modified: 2017-01-31 11:00 UTC
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: david at grudl dot com Assigned: laruence (profile)
Status: Closed Package: Variables related
PHP Version: 5.4.6 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: david at grudl dot com
New email:
PHP Version: OS:


 [2012-08-23 02:08 UTC] david at grudl dot com
Foreach unexpectedly advances the internal array pointer, although array is returned by function without reference.

Test script:

class Test
	function run() 
		$this->arr = array('item');
		var_dump(key($this->arr)); // dumps 0
		// this unexpectedly advances the internal array pointer
		foreach ($this->getArr() as $v) {}
		var_dump(key($this->arr)); // dumps NULL

	function getArr()
		// is NOT returned by reference
		return $this->arr;


$test = new Test;

Expected result:
It dumps "0" in second time too.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-23 15:37 UTC]
-Status: Open +Status: Not a bug
 [2012-08-23 15:37 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

please see the note at :
 [2012-08-24 01:27 UTC] david at grudl dot com
Maybe this is not a bug, but i have read documentations carefully and there is nothing about this. Could you send a link?

And one question: why function reset() uses reference, if there is no need to use reference to advance internal array pointer?
 [2012-08-24 02:51 UTC]
"When foreach first starts executing, the internal array pointer is automatically 
reset to the first element of the array. "  that means foreach change the internal 
potinter of an array.
 [2012-09-13 01:51 UTC] david at grudl dot com
The point is: the iteration is not started on $this->arr, but it is started on array returned from method getArr(). 

Foreach resets internal array pointer of completely different array.
 [2012-09-13 02:44 UTC]
the same, although it is *copy* of a array, but still the same zval, PHP using 
"write on copy".  but the internalPointer's change is not considered as "write" 
 [2012-09-13 02:45 UTC]
s ,write on copy, copy on write,  ;<
 [2012-09-13 16:19 UTC]
@laruence: Changing the array pointer is a change too. That's why foreach usually copies the passed array.

So yes, this is a bug, but from a quick glance at the code I can't yet see why it happens.
 [2012-09-13 16:19 UTC]
-Status: Not a bug +Status: Re-Opened
 [2012-09-14 02:56 UTC]
-Assigned To: +Assigned To: laruence
 [2012-09-14 02:56 UTC]
@nikic,  after we discussed yesterday,  I re-through the codes, do find a 
*inconsistent* place, assign to myself,  thanks
 [2012-09-14 03:00 UTC]
-Status: Re-Opened +Status: Assigned
 [2015-06-10 13:37 UTC] arjen at react dot com
This is fixed in 7.0, still fails in 5.5, 5.6.

Created a different testcase:

When the return value is assigned to a tmp var first, the internal pointer isn't modified.
 [2017-01-31 11:00 UTC]
-Status: Assigned +Status: Closed
 [2017-01-31 11:00 UTC]
Closing as this has been fixed in PHP 7 and PHP 5 is no longer actively supported.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 27 08:03:36 2021 UTC