#45354 [Bgs]: Referencing variable variables impossible

From: Date: Thu, 06 Nov 2008 11:57:28 +0000
Subject: #45354 [Bgs]: Referencing variable variables impossible
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-1401@lists.php.net to get a copy of this message
ID: 45354 User updated by: Sjon at react dot com Reported By: Sjon at react dot com Status: Bogus Bug Type: Documentation problem Operating System: Linux PHP Version: 5.2.6 New Comment: btw: I was surprised that $y_{$a} didn't throw a parser exception; but then I realized that it is recognized as a string-offset construction where character at position $a is returned from the variable $y_ Previous Comments: ------------------------------------------------------------------------ [2008-11-05 16:13:59] Sjon at react dot com Ah, I see now, instead of $x =& $y_{$a}; I should have used $x =& ${'y_'.$a}; Thanks! ------------------------------------------------------------------------ [2008-11-05 15:37:48] torben@php.net 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 ------------------------------------------------------------------------ [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 (#1401) next »