Bug #62901 [Opn->Nab]: foreach unexpectedly advances the internal array pointer

From: Date: Thu, 23 Aug 2012 15:37:33 +0000
Subject: Bug #62901 [Opn->Nab]: foreach unexpectedly advances the internal array pointer
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-172835@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62901&edit=1

 ID:                 62901
 Updated by:         laruence@php.net
 Reported by:        david at grudl dot com
 Summary:            foreach unexpectedly advances the internal array
                     pointer
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Variables related
 PHP Version:        5.4.6
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2012-08-23 02:08:58] 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.



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



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


Thread (12 messages)

« previous php.bugs (#172835) next »