为“$ GLOBALS”构造的变量可变字符串在全球范围内工作,但不在功能范围内工作 [英] Variable variable string constructed for "$GLOBALS" works within global scope, but not function scope

查看:143
本文介绍了为“$ GLOBALS”构造的变量可变字符串在全球范围内工作,但不在功能范围内工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


重要提示: $ GLOBALS 是肮脏和邪恶的。不要使用它们。永远。从来没有。

请关注它不起作用的事实,而不是为什么你会这样做,这纯粹是一个关于技术练习。

这是一个相当奇怪的问题。我试图用一个名为 $ GLOBALS 的字符串构造一个变量变量。



范围



让我们看看在全局范围内 var_dump()如何得到它。 / p>

  $ g = sprintf('%s%s%s%s%s%s%s',chr(71), chr(76),chr(79),chr(66),chr(65),chr(76),chr(83)); 
var_dump($$ g);

结果是一个全局变量数组,您可以看到这里。大!所以,让我们在函数中试试这个。



从函数范围



首先,我们要确保我们可以在一个函数中运行 $ GLOBALS 检查。

 函数globalAllTheThings()
{
var_dump($ GLOBALS);
}

globalAllTheThings();

结果是:有效!您可以在这里看到



现在,让我们尝试我们在全局范围内使用的第一个测试,并在函数中查看会发生什么。



$ p $ function globalAllTheThings()$ (71),chr(76),chr(79),chr(66),chr(66),chr(76),chr(71),brb(
$ g = sprintf('%s%s%s%s%s%s%s' chr(65),chr(76),chr(83));
var_dump($$ g);
}

globalAllTheThings();

为简单起见



您也可以尝试使用而不用奇怪的混淆(不要问)。

  function globalAllTheThings()
{
$ g ='GLOBALS';
var_dump($$ g);
}

globalAllTheThings();

它返回 NULL 。那是什么?为什么它返回 NULL ,我能做些什么才能使它工作。你为什么问?

.jpgalt =ALL THE THINGS,SRSLY>

解决方案


$ b


警告

请注意,变量变量不能用于PHP的Superglobal函数或类方法中的数组。变量 $ this 也是一个不能被动态引用的特殊变量。

http://php.net/manual/en/language.variables.variable.php


这只是特别的。 PHP是特殊的。超全球变种不会像常规变量那样按照相同的规则进行比赛。有人忘了或决定不让它们与函数中的变量变量兼容。期间。

An important note: $GLOBALS are dirty and evil. Don't use them. Ever. Never ever ever.
Please focus on the fact that it doesn't work and not why you would be doing this in the first place, it is purely a theoretical question about a technical exercise.

This is a rather weird one. I'm attempting to construct a variable variable using a string named $GLOBALS.

From the global scope

Let's see what we get when var_dump()ing this in the global scope.

$g = sprintf('%s%s%s%s%s%s%s', chr(71), chr(76), chr(79), chr(66), chr(65), chr(76), chr(83));
var_dump($$g);

The result is an array of global variables, which you can see here. Great! So, let's try this in a function.

From a function scope

First, let's just make sure that we can actually run an $GLOBALS check within a function.

function globalAllTheThings()
{
    var_dump($GLOBALS);
}

globalAllTheThings();

The result is: it works!! You can see this here.

Now, let's try the first test that we used in the global scope, within the function, and see what happens.

function globalAllTheThings()
{
    $g = sprintf('%s%s%s%s%s%s%s', chr(71), chr(76), chr(79), chr(66), chr(65), chr(76), chr(83));
    var_dump($$g);
}

globalAllTheThings();

For simplicity's sake

You can also try this without the weird obfuscation (don't ask).

function globalAllTheThings()
{
    $g = 'GLOBALS';
    var_dump($$g);
}

globalAllTheThings();

It returns NULL. What's that about?? Why does it return NULL, and what can I do to get this working. Why, you ask? For educational purposes of course, and for science!

解决方案

Because the manual says so:

Warning

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.

http://php.net/manual/en/language.variables.variable.php

It's simply "special". PHP is "special". Superglobals don't play by the same rules as regular variables to begin with. Someone forgot to or decided against making them compatible with variable variables in functions. Period.

这篇关于为“$ GLOBALS”构造的变量可变字符串在全球范围内工作,但不在功能范围内工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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