Php variable variable problem

$_POST['asdf'] = 'something'; function test() { // NULL -- not what initially expected $string = '_POST'; echo '===='; var_dump(${$string}); echo '===='; // Works as expected echo '++++++'; var_dump(${'_POST'}); echo '++++++'; // Works as expected global ${$string}; var_dump(${$string}); } // Works as expected $string = '_POST'; var_dump(${$string}); test(); 

I don’t understand why this behavior .. can someone explain .. I need to know why this behavior. I really don't get the code.

+4
source share
2 answers

PHP has no real global variables. "Superglobals" are also wrong. $_POST and $_GET never present in the hash tables of a local variable. They exist as aliases that PHP sees only for regular accesses. The access method of a variable variable always looks only at the current local hash table.

 global $$string; // $$string = & $GLOBALS[$string]; 

This is a great trick for creating a link to superglobal in a local hash table. That is why, after this statement, you can use variable variables to access "superglobals."

+4
source

Look here

Note that variable variables cannot be used with PHP Superglobal arrays inside functions or classes of methods. The $ this variable is also a special variable that cannot be dynamically referenced.

+10
source

All Articles