尝试从字符串名称访问$ _SERVER(或任何全局)变量 [英] Trying to access $_SERVER(or any global) variable from string name

查看:141
本文介绍了尝试从字符串名称访问$ _SERVER(或任何全局)变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我遇到了这样可怕的情况. 看来此错误与PHP有关.

Today I met such a terrible situation. It seems this bug is related to PHP.

我正在尝试从字符串名称访问$_SERVER或其他超级全局变量.

I'm trying to access to $_SERVER or another super global variables but from string name.

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.

那里发生了什么?有人可以解释这种情况.

What is going on there? Can someone explain this situation.

推荐答案

变量不能在函数或类方法中与PHP的Superglobal数组一起使用. 演示

variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. Demo

请参考 php文档变量变量

请注意,变量变量不能与PHP一起使用 函数或类方法中的超全局数组.变量 $ this也是无法引用的特殊变量 动态地

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.

这篇关于尝试从字符串名称访问$ _SERVER(或任何全局)变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆