note 101425 deleted from control-structures.foreach by googleguy
| From: | googleguy@php.net | Date: | Wed, 14 Nov 2012 22:22:54 +0000 |
| Subject: | note 101425 deleted from control-structures.foreach by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191950@lists.php.net to get a copy of this message | ||
Note Submitter: Mark Rose
----
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!