note 34288 deleted from language.references by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 01:19:23 +0000 |
| Subject: | note 34288 deleted from language.references by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57321@lists.php.net to get a copy of this message | ||
Note Submitter: ernest@vogelsinger.at
----
References have an interesting side effect in arrays.
Check this code snippet:
error_reporting(E_ALL); // make notices visible
$ar = array();
// this will raise a notice about an undefined index:
$test = $ar['sample1'];
// why? have a look at the array now:
echo '<pre>'; print_r($ar); echo '</pre>';
// you see there's no 'sample' array entry.
// this output will be empty:
echo "Array content is '$test'<br />";
// assigning to the array will not change the output, as expected:
$ar['sample1'] = 'demo';
echo "Array content is '$test'<br />";
// using a reference gives a complete other picture:
// this reference assignment will NOT trigger any notice!
$test =& $ar['sample2'];
// why? have a look at the array now:
echo '<pre>'; print_r($ar); echo '</pre>';
// you see the array entry now magically came into existence!
// now let's assign some value to it:
$ar['sample2'] = 'another demo';
// and the test variable will automagically reference this entry:
echo "Array content is '$test'<br />";
Summary: referencing a non-existing array entry will automatically define it and establish a
reference to the (initially empty) entry.