php.net |  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
Votes:1
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: david at grudl dot com
New email:
PHP Version: OS:

 

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

Test script:
---------------
<?php


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;
$test->run();


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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-23 15:37 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2012-08-23 15:37 UTC] laruence@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

please see the note at : http://us3.php.net/manual/en/control-
structures.foreach.php
 [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] laruence@php.net
"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.

http://us3.php.net/manual/en/control-structures.foreach.php
 [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] laruence@php.net
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" 
here.
 [2012-09-13 02:45 UTC] laruence@php.net
s ,write on copy, copy on write,  ;<
 [2012-09-13 16:19 UTC] nikic@php.net
@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] nikic@php.net
-Status: Not a bug +Status: Re-Opened
 [2012-09-14 02:56 UTC] laruence@php.net
-Assigned To: +Assigned To: laruence
 [2012-09-14 02:56 UTC] laruence@php.net
@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] laruence@php.net
-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: http://3v4l.org/jH4pM

When the return value is assigned to a tmp var first, the internal pointer isn't modified.
 [2017-01-31 11:00 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2017-01-31 11:00 UTC] nikic@php.net
Closing as this has been fixed in PHP 7 and PHP 5 is no longer actively supported.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Dec 06 12:01:24 2019 UTC