无法通过函数中的变量变量访问超全局变量? [英] Superglobals can't be accessed via variable variables in a function?

查看:82
本文介绍了无法通过函数中的变量变量访问超全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过函数内部的变量访问超全局变量.我是问题的根源,还是PHP的精妙之处之一?以及如何绕过它?

I can't access superglobals via variable variables inside a function. Am I the source of the problem or is it one of PHP's subtleties? And how to bypass it?

print_r(${'_GET'});

工作正常

$g_var = '_GET';
print_r(${$g_var});

给我通知:未定义的变量:_GET

Gives me a Notice: Undefined variable: _GET

推荐答案

PHP无法识别这是全局变量访问:
它将$_GET${'_GET'}编译为相同的操作码序列,即global FETCH_R.另一方面,${$g_var}将导致local FETCH_R.

PHP isn't able to recognize that this is a global variable access:
It compiles $_GET and ${'_GET'} to the same opcode sequence, namely a global FETCH_R. ${$g_var} on the other hand will result in a local FETCH_R.

文档:

Superglobals不能用作函数或类方法中的变量变量.

Superglobals cannot be used as variable variables inside functions or class methods.

这篇关于无法通过函数中的变量变量访问超全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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