Bug #73160 [Opn->Nab]: foreach var reference incorrect last entry value

From: Date: Sat, 24 Sep 2016 09:58:20 +0000
Subject: Bug #73160 [Opn->Nab]: foreach var reference incorrect last entry value
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-204234@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73160&edit=1 ID: 73160 Updated by: requinix@php.net Reported by: florian dot bailly at gmx dot com Summary: foreach var reference incorrect last entry value -Status: Open +Status: Not a bug Type: Bug Package: *General Issues Operating System: Mac OSX / Unix PHP Version: 7.1.0RC2 Block user comment: N Private report: N 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 Please red the big red warning in the documentation. http://php.net/manual/en/control-structures.foreach.php Previous Comments: ------------------------------------------------------------------------ [2016-09-24 09:38:47] florian dot bailly at gmx dot com 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 this bug report at https://bugs.php.net/bug.php?id=73160&edit=1

« previous php.bugs (#204234) next »