Bug #62123 [Opn->Nab]: Foreach changes the internal pointer of the array strangely
Edit report at https://bugs.php.net/bug.php?id=62123&edit=1
ID: 62123
Updated by: laruence@php.net
Reported by: a dot protaskin at gmail dot com
Summary: Foreach changes the internal pointer of the array
strangely
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: Linux
PHP Version: 5.4.3
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
Previous Comments:
------------------------------------------------------------------------
[2012-05-23 23:59:53] a dot protaskin at gmail dot com
Thanks for your answer, nikic. I understood this behaviour, this is not the bug.
------------------------------------------------------------------------
[2012-05-23 21:54:57] nikic@php.net
See http://stackoverflow.com/a/8263425/385378 for
an explanation.
------------------------------------------------------------------------
[2012-05-23 13:23:38] a dot protaskin at gmail dot com
Description:
------------
Traversing the array changes once the internal pointer by 1.
Traversing the array by link changes the internal pointer just as with each().
Test script:
---------------
<?php
$array = [0, 1, 2];
while (each($array)) {
var_dump(key($array));
}
reset($array);
echo "----\n";
foreach ($array as $elem) {
var_dump(key($array));
}
reset($array);
echo "----\n";
$array2 = &$array;
foreach ($array2 as $elem) {
var_dump(key($array));
}
Expected result:
----------------
int(1)
int(2)
NULL
----
int(1)
int(2)
NULL
----
int(1)
int(2)
NULL
Actual result:
--------------
int(1)
int(2)
NULL
----
int(1)
int(1)
int(1)
----
int(1)
int(2)
NULL
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62123&edit=1
Thread (4 messages)