php variable variables problem

后端 未结 2 1112
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 05:23
$_POST[\'asdf\'] = \'something\';

function test() {
    // NULL -- not what initially expected
    $string = \'_POST\';
    echo \'====\';
    var_dump(${$string});         


        
相关标签:
2条回答
  • 2021-01-19 05:59

    Look at here

    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.

    0 讨论(0)
  • 2021-01-19 06:17

    PHP does not have real global variables. The "superglobals" are also a misnomer. $_POST and $_GET are never present in the local variable hash tables. They exist as aliases, which PHP only sees for ordinary accesses. The variable variable access method only ever looks into the current local hash table.

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

    Is a nifty trick to create a reference to the superglobals in the local hash table. This is why after that statement, you are able to use variable variables to access the "superglobals".

    0 讨论(0)
提交回复
热议问题