From: tomas_matousek at hotmail dot com Operating system: WinXP PHP version: 5.0.1 PHP Bug Type: Arrays related Bug description: Nondeterministic behavior of array inner pointer
Description: ------------ In the manual page describing each() function is the following caution: "Because assigning an array to another variable resets the original arrays pointer, ..." I think that this is a bug, although it is documented and treated as feature. I realized that the pointer is reset when a copy of an array is made. However, PHP makes some optimizations which prevents unnecessary array copying (which is good feature). Since these optimizations are not known for users (and shouldn't be) the behavior of inner pointer is thus non-deterministic from user's point of view. See the follwoing code. Its output depends on whether the statement $b[] = 1; is commented or not. Reproduce code: --------------- function f($a) { $b = $a; /*$b[] = 1;*/ return $b; } $arrayOuter = array("key1","key2"); $arrayInner = array("0","1"); while(list(,$o) = each($arrayOuter)) { $q = f($arrayInner); while(list(,$i) = each($arrayInner)) { print "inloop $i for $o\n"; } } Expected result: ---------------- inloop 0 for key1 inloop 1 for key1 Actual result: -------------- inloop 0 for key1 inloop 1 for key1 inloop 0 for key2 inloop 1 for key2 -- Edit bug report at http://bugs.php.net/?id=29950&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=29950&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=29950&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=29950&r=trysnapshot51 Fixed in CVS: http://bugs.php.net/fix.php?id=29950&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=29950&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=29950&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=29950&r=needscript Try newer version: http://bugs.php.net/fix.php?id=29950&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=29950&r=support Expected behavior: http://bugs.php.net/fix.php?id=29950&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=29950&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=29950&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=29950&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=29950&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=29950&r=dst IIS Stability: http://bugs.php.net/fix.php?id=29950&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=29950&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=29950&r=float