note 101489 added to control-structures.foreach

From: Date: Mon, 20 Dec 2010 17:36:15 +0000
Subject: note 101489 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-175080@lists.php.net to get a copy of this message
@Mark Rose As noted above by a giant exclimation point sign, you must destroy the value using unset($value); to ensure an issue like that does not happen. Using your same code with a slight change to include the unset on $bar produces a correct result: <?php $foo = array('one', 'two', 'three'); foreach ($foo as &$bar) { // no-op } var_dump($foo); unset($bar); foreach ($foo as $bar) { // no-op } var_dump($foo); unset($bar); ?> Outputs: 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(5) "three" } ---- Server IP: 69.147.83.197 Probable Submitter: 75.5.235.205 ---- Manual Page -- http://www.php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/101489 Del: integrated -- https://master.php.net/note/delete/101489/integrated Del: useless -- https://master.php.net/note/delete/101489/useless Del: bad code -- https://master.php.net/note/delete/101489/bad+code Del: spam -- https://master.php.net/note/delete/101489/spam Del: non-english -- https://master.php.net/note/delete/101489/non-english Del: in docs -- https://master.php.net/note/delete/101489/in+docs Del: other reasons-- https://master.php.net/note/delete/101489 Reject -- https://master.php.net/note/reject/101489 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175080) next »