与 eval 函数相反的是什么?

What's opposite of eval function?

我想知道如何使用与 eval 函数完全相反的函数。

这是我的代码:

$test = 1;

$t = '$test';

echo opposite_eval($t);

我必须从上面的代码中1输出,我该如何使用方法、函数或class?

感谢各位朋友的帮助!

我通过从 $t 字符串中删除 $ 来作弊(你可以在函数中做到这一点它只是一个字符串:

$t = 'test';

function opposite_eval($t){
$test = 1;
return($$t);

}

echo opposite_eval($t); //=1

您要查看的阶段是 variable variables

我想你想要一个 variables variable

你的情况是:

$test = 1;
$t = 'test';
echo $$t;
// output: 1

插件:
您也可以这样做:

$test['x'] = 1;
$t = 'test';
echo $$t['x'];

这个工作:

$test['x'] = 1;
$t = "test['x']";
echo $$t;
// Produces: NOTICE Undefined variable: test['x'] on line number 6

也不会:

$test = new stdClass();
$test->x = 1;
$t = "test->x";
echo $$t;

但这会起作用:

$test = new stdClass();
$test->x = 1;
$t = "{$test->x}";
echo $t;

这也适用:

$test =[];
$test['x'] = 1;
$t = "{$test['x']}";
echo $t;