0

有没有办法可以将原始文本更改为回显中的变量名。例如将变量设置$numbernumber1,然后当它回显时使用$number1?

为了澄清,假设我们有一个名为的变量$number = "number1";,当我们回显时$number,它回显了一个名为的变量number1,我将如何更改number1为一个变量?像 "echo "$"."$number"; 之类的东西?

4

3 回答 3

1

您可以尝试以下方法

$number = 2012 ;
$name = 'number' ;
echo $$name ; //2012
echo ${$name};  //2012
echo ${"number"} ;  //2012
echo "${$name}";  //2012
于 2012-09-24T23:14:02.757 回答
0

You can do this with two dollars instead of one. Note that this is really bad practice, and there's probably a better way to achieve your goal here.

$number = 42
$usenumber = 'number'
echo $$usenumber //echos 42
于 2012-09-24T23:11:56.400 回答
0

很容易:

$number = 'number1';
$$number = '';
var_dump(get_defined_vars());

或者

echo $number1;
于 2012-09-24T23:07:30.713 回答