#28939 [NEW]: Invalid array internal pointer behavior in foreach statement

From: Date: Mon, 28 Jun 2004 07:56:50 +0000
Subject: #28939 [NEW]: Invalid array internal pointer behavior in foreach statement
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-61396@lists.php.net to get a copy of this message
From: tomas_matousek at hotmail dot com Operating system: WinXP PHP version: 5.0.0RC3 PHP Bug Type: Scripting Engine problem Bug description: Invalid array internal pointer behavior in foreach statement 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 bug report at http://bugs.php.net/?id=28939&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=28939&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=28939&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=28939&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=28939&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=28939&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=28939&r=needscript Try newer version: http://bugs.php.net/fix.php?id=28939&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=28939&r=support Expected behavior: http://bugs.php.net/fix.php?id=28939&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=28939&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=28939&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=28939&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=28939&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=28939&r=dst IIS Stability: http://bugs.php.net/fix.php?id=28939&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=28939&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=28939&r=float

« previous php.bugs (#61396) next »