我试图弄清楚用于从另一个变量引用变量的正确语法或函数。例如,
$_SESSION['foo'] = 'bar' ;
$var1 = '$_SESSION[\'foo\']' ;
$var2 = ${"$var1"} ;
echo "$var2" ;
如何显示值'bar'?
答案 0 :(得分:0)
要引用变量,请在变量之前使用&符号&
,如下所示:
$_SESSION['foo'] = 'bar' ;
$var1 = '$_SESSION[\'foo\']' ;
$var2 = &$var1;
echo "$var2" ;
答案 1 :(得分:0)
使用引用变量:
$_SESSION['foo'] = 'bar';
$var1 = $_SESSION['foo'];
$var2 = &$_SESSION['foo'];
$_SESSION['foo'] = 'abc';
echo $var1; // prints bar
echo $var2; // prints abc
或许这就是你想要的:
$_SESSION['foo'] = 'bar';
$var1 = '$_SESSION[\'foo\']';
$var2 = eval("return $var1;"); // The argument to eval has to be a statement
echo $var2; // prints bar