|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-02-15 12:48 UTC] felipensp at gmail dot com
Description:
------------
Undefined variables non-reference/reference as item of an array has different behavior.
Reproduce code:
---------------
1:
felipe@felipe:~/php5$ sapi/cli/php -r 'var_dump($a =array($a));'
2:
felipe@felipe:~/php5$ sapi/cli/php -r 'var_dump(array(&$a));'
3:
felipe@felipe:~/php5$ sapi/cli/php -r 'var_dump($a =array(&$a));'
Expected result:
----------------
2 and 3 as same result.
Actual result:
--------------
1:
array(1) {
[0]=>
NULL
}
2:
array(1) {
[0]=>
&NULL
}
3:
array(1) {
[0]=>
&array(1) {
[0]=>
&array(1) {
[0]=>
*RECURSION*
}
}
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 20:00:01 2025 UTC |
Your expectation is wrong: In your second example two variables refer to the same NULL value. (first element of array and variable $a) Compare the output of the following two scripts <?php $x = array(&$a); var_dump($x); ?> Actual result: -------------- array(1) { [0]=> &NULL } <?php $x = array(&$a); unset($a); var_dump($x); ?> Actual result: -------------- array(1) { [0]=> NULL } In the third example you assign the array from second example to $a, but $a is a reference, so the assignment will update the first element of array too. <?php $b = array(&$a); var_dump($b); $a = $b; var_dump($b); ?> Actual result: -------------- array(1) { [0]=> &NULL } array(1) { [0]=> &array(1) { [0]=> &array(1) { [0]=> *RECURSION* } } } All results are the same as they should be.