#45354 [Bgs]: Referencing variable variables impossible

From: Date: Wed, 05 Nov 2008 15:37:49 +0000
Subject: #45354 [Bgs]: Referencing variable variables impossible
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-1364@lists.php.net to get a copy of this message
ID: 45354 Updated by: torben@php.net Reported By: Sjon at react dot com Status: Bogus Bug Type: Documentation problem Operating System: Linux PHP Version: 5.2.6 New Comment: In addition to what Jakub said, variable variables can indeed be assigned by reference in PHP. The problem with the given code example is not that PHP can't assign variable variables by reference, but that the example doesn't make any sense. You're trying to concatenate the value in $a to the end of the value in $y_, which has not been given a value. If you define $y_ properly and use correct syntax then the example will work. See the following examples: <?php $foo = 'I am foo'; $bar = 'foo'; $baz =& $$bar; echo "Ref to varvar: $baz\n"; $y_ = 'y_'; $y_a = array(); $a = 'a'; $x =& ${$y_.$a}; $x['c'] = 'd'; var_dump($y_a); ?> Torben Previous Comments: ------------------------------------------------------------------------ [2008-11-05 11:02:20] vrana@php.net $y_{$a} itself is not documented and it's such a weird construction that it is undocumented by purpose. ------------------------------------------------------------------------ [2008-06-25 12:57:32] Sjon at react dot com Description: ------------ It seems to be impossible to create a reference to a variable variable, even though this is not documented anywhere as a known bug or limitation Reproduce code: --------------- $y_a=array(); $a='a'; $x =& $y_{$a}; #DOES NOT WORK # $x =& $y_a; #DOES WORK $x['c'] = 'd'; var_dump($y_a); Expected result: ---------------- array(1) { ["c"]=> string(1) "d" } Actual result: -------------- array(0) { } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45354&edit=1

« previous php.doc.bugs (#1364) next »