Bug #73160 [NEW]: foreach var reference incorrect last entry value

From: Date: Sat, 24 Sep 2016 09:38:49 +0000
Subject: Bug #73160 [NEW]: foreach var reference incorrect last entry value
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-204232@lists.php.net to get a copy of this message
From: florian dot bailly at gmx dot com Operating system: Mac OSX / Unix PHP version: 7.1.0RC2 Package: *General Issues Bug Type: Bug Bug description:foreach var reference incorrect last entry value Description: ------------ The first foreach changes the value by reference of each entry by adding the digit "2". The next foreach dumps each key with its entry. We can see that the last entry is equal to "titi2" and not "tutu2" (so it appears to be equal to the previous entry). The last commented out foreach demonstrates that if the var used for the entry is different ($val <> $val2), then there's no bug. But you must comment out the second foreach first. Test script: --------------- <?php $a = [ 'toto', 'titi', 'tutu', ]; foreach ($a as $key => &$val) $val .= '2'; foreach ($a as $key => $val) var_dump($key, $val); // foreach ($a as $key => $val2) // var_dump($key, $val2); Actual result: -------------- int(0) string(5) "toto2" int(1) string(5) "titi2" int(2) string(5) "titi2" -- Edit bug report at https://bugs.php.net/bug.php?id=73160&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=73160&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=73160&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=73160&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=73160&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=73160&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=73160&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=73160&r=needscript Try newer version: https://bugs.php.net/fix.php?id=73160&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=73160&r=support Expected behavior: https://bugs.php.net/fix.php?id=73160&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=73160&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=73160&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=73160&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73160&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=73160&r=dst IIS Stability: https://bugs.php.net/fix.php?id=73160&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=73160&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=73160&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=73160&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=73160&r=mysqlcfg

« previous php.bugs (#204232) next »