Trying to access $_SERVER(or any global) variable from string name [duplicate]

馋奶兔 提交于 2019-11-28 09:31:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!