尝试从字符串名称访问 $_SERVER(或任何全局)变量
Trying to access $_SERVER(or any global) variable from string name
今天遇到这么惨的情况。
这个错误似乎与 PHP
.
有关
我正在尝试访问 $_SERVER
或其他超级全局变量,但来自字符串名称。
此版本的实施有效。
var_dump(${"_SERVER"}); // working
但是当尝试使用变量执行此操作时,会收到找不到变量的通知。
$var_name = "_SERVER";
var_dump(${$var_name}); // Notice</b>: Undefined variable: _SERVER in...
And this will happen only with a global variable.
那里发生了什么事?谁能解释一下这种情况。
可变变量不能在函数或class方法中与PHP的超全局数组一起使用。 Demo
Please note that variable variables cannot be used with PHP's
Superglobal arrays within functions or class methods. The variable
$this is also a special variable that cannot be referenced
dynamically.
今天遇到这么惨的情况。
这个错误似乎与 PHP
.
我正在尝试访问 $_SERVER
或其他超级全局变量,但来自字符串名称。
此版本的实施有效。
var_dump(${"_SERVER"}); // working
但是当尝试使用变量执行此操作时,会收到找不到变量的通知。
$var_name = "_SERVER";
var_dump(${$var_name}); // Notice</b>: Undefined variable: _SERVER in...
And this will happen only with a global variable.
那里发生了什么事?谁能解释一下这种情况。
可变变量不能在函数或class方法中与PHP的超全局数组一起使用。 Demo
Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.