note 34288 added to language.references
| From: | ernest at vogelsinger dot at | Date: | Mon, 21 Jul 2003 13:13:29 +0000 |
| Subject: | note 34288 added to language.references | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-52453@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/language.references.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34288
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34288&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34288&report=yes