csskevin
csskevin

Reputation: 383

Variable Variable for $_GET

i have following code.

$hello = "World";
$test = "hello";
echo $$test;

When I execute this I get as result: World

So far as good

But when I use a reserved variable, for example $_GET it doesn't work.

$test = "_GET";
var_dump($$test);

Here the result is NULL. Is there a way to get values of a reserved variable throught a variable variable?

Upvotes: 2

Views: 53

Answers (1)

Alex Howansky
Alex Howansky

Reputation: 53533

Superglobals can only be dereferenced by variable variables in the global scope. The fact you can't get it to work seems to indicate that your code is in a function/method. In this case, you can use the $GLOBALS superglobal:

function foo() {
    $str = '_GET';
    var_dump($GLOBALS[$str]);
}

foo();

Upvotes: 2

Related Questions