note 10012 added to What References Do
| From: | brichardson at lineone dot net | Date: | Thu, 07 Dec 2000 01:50:03 +0000 |
| Subject: | note 10012 added to What References Do | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-8490@lists.php.net to get a copy of this message | ||
Be careful with references and arrays. If $array contains the key "red" with the value
"apple" then this code:
$tmp =& $array["red"];
echo $tmp;
will give the ouput apple. But if there is no such key in the array, then the above code will NOT
ONLY leave $tmp unset (as you would expect) BUT ALSO create the key "red" with an unset
value (which you might not expect).
To be clearer, this code:
unset($array);
$tmp =& $array["red"];
reset($array);
echo key($array)."<br>";
$tmp = "apple";
echo $array["red"]."<br>";
will give this output:
red
apple
http://www.php.net/manual/language.references.whatdo.php