Bug #75652 [Com]: foreach strange error

From: Date: Mon, 11 Dec 2017 00:29:00 +0000
Subject: Bug #75652 [Com]: foreach strange error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213047@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75652&edit=1 ID: 75652 Comment by: a at b dot c dot de Reported by: nikolay at rockstonedev dot com Summary: foreach strange error Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Gentoo PHP Version: 7.2.0 Block user comment: N Private report: N New Comment: See also the big red WARNING on the foreach manual page www.php.net/control-structures.foreach Previous Comments: ------------------------------------------------------------------------ [2017-12-08 10:43:32] daverandom@php.net As a general rule, the best advice is not to use foreach by reference. If you have a legitimate case for doing so, best practice to avoid unexpected results is to do at least one of the following: 1) Do not re-use the iteration variable ($value in your code sample) again in the same scope. https://3v4l.org/b6CN0 2) Break the reference chain explicitly by calling unset($value) as soon as you have finished your use for the reference - in your sample code this would be immediately after the first loop. https://3v4l.org/nldtj ------------------------------------------------------------------------ [2017-12-08 10:41:51] danack@php.net There's an explanation of what is happening on this bug https://bugs.php.net/bug.php?id=74751 tl:dr, the reference carries over from one foreach to the next. Re-using variable names when they have been used as a reference, without having an explicit 'unset' is inherently 'surprising'. ------------------------------------------------------------------------ [2017-12-08 10:38:32] daverandom@php.net 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 After the first loop, $value remains a reference to the last element in the array. The second loop assigns each element in the array to the last element of the array. On the last iteration of the second loop, $value remains a reference to the previous element (which is now 4 because of the increment in the first loop). ------------------------------------------------------------------------ [2017-12-08 10:32:46] nikolay at rockstonedev dot com Description: ------------ Using foreach with & for getting address of variable changes last element of array in next foreach Test script: --------------- <?php $values = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4); $sum = 0; foreach ($values as &$value) $value++; foreach ($values as $value) $sum += $value; print_r($values); ?> Expected result: ---------------- Array ( [a] => 2 [b] => 3 [c] => 4 [d] => 5 ) Actual result: -------------- Array ( [a] => 2 [b] => 3 [c] => 4 [d] => 4 ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75652&edit=1

« previous php.bugs (#213047) next »