|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-03-03 12:31 UTC] avb@php.net
Description:
------------
If an array is passed by value to the function and is iterated over within said function, the "internal pointer" of the original array is moved.
Reproduce code:
---------------
function doForeach($array)
{
foreach ($array as $k => $v) {
// do stuff
}
}
$foo = array('foo', 'bar', 'baz');
doForeach($foo);
var_dump(key($foo));
Expected result:
----------------
int(0) (returned by versions prior to 5.2.1)
Actual result:
--------------
NULL (returned by version 5.2.1 and current 5.2 snapshot)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 10:00:02 2025 UTC |
This problem occurs in copies of an object's properties too. The array pointer is advanced by foreach in both the original and the copied variable. <?php class foo { var $arr = array(1, 2, 3); function test() { var_dump(key($this->arr)); $array_copy = $this->arr; foreach ($array_copy as $val) {} } } $a = new foo(); $a->test(); $a->test(); ?>