问题
Today I met such a terrible situation.
It seems this bug is related to PHP
.
I'm trying to access to $_SERVER
or another super global variables but from string name.
This version of implementation is working.
var_dump(${"_SERVER"}); // working
But when trying to do this with variable then receiving notice that variable not found.
$var_name = "_SERVER";
var_dump(${$var_name}); // Notice</b>: Undefined variable: _SERVER in...
And this will happen only with a global variable.
What is going on there? Can someone explain this situation.
回答1:
variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. Demo
Refer to php doc Variable variables
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.
来源:https://stackoverflow.com/questions/58832141/trying-to-access-serveror-any-global-variable-from-string-name