#28939 [Opn->Bgs]: Invalid array internal pointer behavior in foreach statement
| From: | tony2001@php.net | Date: | Tue, 15 Feb 2005 00:30:31 +0000 |
| Subject: | #28939 [Opn->Bgs]: Invalid array internal pointer behavior in foreach statement | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-74072@lists.php.net to get a copy of this message | ||
ID: 28939
Updated by: tony2001@php.net
Reported By: tomas_matousek at hotmail dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: WinXP
PHP Version: 5.0.0RC3
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
"Also note that foreach operates on a copy of the specified array and
not the array itself. Therefore, the array pointer is not modified as
with the each() construct, and changes to the array element returned
are not reflected in the original array. However, the internal pointer
of the original array is advanced with the processing of the array.
Assuming the foreach loop runs to completion, the array's internal
pointer will be at the end of the array."
(c) http://php.net/manual/en/control-structures.foreach.php
Previous Comments:
------------------------------------------------------------------------
[2004-06-28 09:56:48] tomas_matousek at hotmail dot com
Description:
------------
Array's internal pointer (which one can manipulate by next, prev,
current, etc. functions) has invalid bahavior in foreach statement.
Moreover, there is a contradictional description of this behavior in
the manual.
from manual:
"Note:
Also note that foreach operates on a copy of the specified array and
not the array itself. Therefore, the ARRAY POINTER IS NOT MODIFIED as
with the each() construct, and changes to the array element returned
are not reflected in the original array. However, the INTERNAL POINTER
OF THE ORIGINAL ARRAY IS ADVANCED with the processing of the array.
Assuming the foreach loop runs to completion, the array's internal
pointer will be at the end of the array.
"
It is not clear whether or not the internal pointer of the original
array is advanced during enumeration.
I made some tests and it seems to me that foreach statement mishandles
array's internal pointer (see "reproducable code").
I would prefer if the original array's pointer was not influenced by
foreach statement because one can do changes in the array during
enumeration (which is legal since it is assumed that foreach operates
on a copy) and IMHO any changes to the pointer in original array would
necessarily lead to strange behavior.
Reproduce code:
---------------
echo "-- a --\n";
$a = array(0,1,2);
var_dump(current($a));
foreach ($a as $value)
{
/* do not touch $a */
}
var_dump(current($a));
$b = array(0,1,2);
echo "-- b --\n";
var_dump(current($b));
foreach ($b as $value)
{
var_dump(current($b));
}
var_dump(current($b));
Expected result:
----------------
either the following (pointer is advanced):
-- a --
int(0)
bool(false)
-- b --
int(0)
int(0)
int(1)
int(2)
int(false)
or the following (pointer is not avanced):
-- a --
int(0)
int(0)
-- b --
int(0)
int(0)
int(0)
int(0)
int(0)
Actual result:
--------------
-- a --
int(0)
bool(false)
-- b --
int(0)
int(0)
int(0)
int(0)
int(0)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=28939&edit=1