PHP $ _GET/$ _ POST通过变量变量 [英] PHP $_GET/$_POST via variable variables

查看:45
本文介绍了PHP $ _GET/$ _ POST通过变量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用变量变量来动态访问$ _GET和$ _POST数组.我尝试这样做的原因是,我可以在多个数组上执行类似的操作,而无需为每个数组声明特定的迭代.我有理由肯定这是可行的,因为PHP的文档说它能够使用变量变量来动态访问数组,但是我无法使其工作.一个简单的演示是当我尝试验证是否已设置某个属性时.

I'm attempting to dynamically access both the $_GET and $_POST arrays, among others, using variable variables. The reason I'm trying to do this is so that I can perform similar actions on multiple arrays without needing to declare specific iterations for each. I'm reasonably sure this is possible, as PHP's documentation says it is able to use variable variables to dynamically access an array, however I'm unable to get it to work. A simple demonstration is when I'm attempting to verify that a certain property has been set.

if(isset(${$this->_array}[$property])) { return ${$this->_array}[$property]; }
else { return null; }

当我运行上面的脚本时,我总是得到null,但是当我手动地使用$ _GET或$ _POST静态搜索所需的属性时,就会得到所需的结果.我对$this->_array$property进行了三重检查,它们返回正确的字符串值.这些数组是否不可用于此类访问,还是我做错了什么?

When I run the above script I always get null, however when I statically seek the desired property, manually using $_GET or $_POST, I get the desired outcome. I have triple checked $this->_array and $property and they are returning the correct string values. Are these arrays unavailable for such access, or am I doing something wrong?

推荐答案

Superglobals(例如$_POST)

Superglobals (such as $_POST) can not be used as variable variables within functions.

您可以说诸如$post = $_POST;之类的内容,然后使用'post',它会起作用,但直接使用'_POST'不会.

You could say something like $post = $_POST; and then use 'post' and it'd work, but directly using '_POST' won't.

这篇关于PHP $ _GET/$ _ POST通过变量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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