note 101489 deleted from control-structures.foreach by danbrown
| From: | danbrown@php.net | Date: | Mon, 20 Dec 2010 17:48:05 +0000 |
| Subject: | note 101489 deleted from control-structures.foreach by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-175081@lists.php.net to get a copy of this message | ||
Note Submitter: faker at faking dot com
----
@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"
}