Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

ALL THE THINGS, SRSLY

share|improve this question
2  
For simplicity's sake, can't you write $g = 'GLOBALS' instead of your weird sprintf-chr? It shouldn't influence the result. If it did, that'd be worth a whole 'nother question. –  deceze Aug 13 '13 at 9:50
3  
Because is PHP. –  Ignacio Vazquez-Abrams Aug 13 '13 at 9:51
2  
Because ${'string'} looks for variable in given scope. –  Leri Aug 13 '13 at 9:52
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
 
Wow, nice find. I did "RTFM" on this but must have missed that. What a fail. –  Jimbo Aug 13 '13 at 9:56
3  
...where "special" is used in highly politically incorrect sense. –  DaveRandom Aug 13 '13 at 9:56
 
@Dave PHP is the language among programming languages which is mostly defined by its exceptions, not its rules... ;) –  deceze Aug 13 '13 at 9:57
 
Why then code $f = '_POST'; var_dump($$f); works properly? _POST is also a superglobal. –  Alma Do Aug 13 '13 at 9:58
 
@Alma Does it now? codepad.org/4lcRowDG –  deceze Aug 13 '13 at 9:59
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.