note 101425 added to control-structures.foreach

From: Date: Wed, 15 Dec 2010 22:55:32 +0000
Subject: note 101425 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-174968@lists.php.net to get a copy of this message
Be careful recycling the $value variable if using a reference! I recently ran into a strange bug, where the last item of a second foreach loop was referenced. <?php $foo = array('one', 'two', 'three'); foreach ($foo as &$bar) { // no-op } var_dump($foo); foreach ($foo as $bar) { // no-op } var_dump($foo); ?> produces: array(3) { [0]=>string(3) "one" [1]=>string(3) "two" [2]=>&string(5) "three" } array(3) { [0]=>string(3) "one" [1]=>string(3) "two" [2]=>&string(3) "two" } Note that the last value in the array gets set to the second last value! ---- Server IP: 216.235.13.26 Probable Submitter: 69.165.243.2 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/101425 Del: integrated -- https://master.php.net/note/delete/101425/integrated Del: useless -- https://master.php.net/note/delete/101425/useless Del: bad code -- https://master.php.net/note/delete/101425/bad+code Del: spam -- https://master.php.net/note/delete/101425/spam Del: non-english -- https://master.php.net/note/delete/101425/non-english Del: in docs -- https://master.php.net/note/delete/101425/in+docs Del: other reasons-- https://master.php.net/note/delete/101425 Reject -- https://master.php.net/note/reject/101425 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#174968) next »