Bug #81350 [NEW]: foreach returns unexpected result

From: Date: Wed, 11 Aug 2021 12:37:03 +0000
Subject: Bug #81350 [NEW]: foreach returns unexpected result
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235761@lists.php.net to get a copy of this message
From: wilhelm at blueend dot com Operating system: CentOS PHP version: 7.4.22 Package: Variables related Bug Type: Bug Bug description:foreach returns unexpected result Description: ------------ If you have two foreach loops based on the same array and the first loop uses the variable as reference the second loop produces an unexpected result if the variable name for the value stays the same. Test script: --------------- $t = [1,2,3]; foreach ($t as &$testVar){ print_r($testVar); } foreach ($t as $testVar){ print_r($testVar); } Expected result: ---------------- I would expect the same output as if I would have used the non reference variant in the first place. I would expect => (Loop 1) 1,2,3 => (Loop 2) 1,2,>3< Actual result: -------------- I get => (Loop 1) 1,2,3 => (Loop 2) 1,2,>2< -- Edit bug report at https://bugs.php.net/bug.php?id=81350&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=81350&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=81350&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=81350&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=81350&r=needscript Try newer version: https://bugs.php.net/fix.php?id=81350&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=81350&r=support Expected behavior: https://bugs.php.net/fix.php?id=81350&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=81350&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=81350&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=81350&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=81350&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=81350&r=dst IIS Stability: https://bugs.php.net/fix.php?id=81350&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=81350&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=81350&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=81350&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=81350&r=mysqlcfg

« previous php.bugs (#235761) next »