Make all variables global, PHP

后端 未结 6 827
悲&欢浪女
悲&欢浪女 2020-12-19 03:23

Is there a way to make all variables global?

6条回答
  •  忘掉有多难
    2020-12-19 04:00

    You Can simple make them a reference to $GLOBALS

    foreach($GLOBALS as $k => $v)
       $$k=&$GLOBALS[$k];
    

    Explanation of the code:

    $GLOBLAS is a superglobal variable (visible everywhere). Basicly it contains all variables


    $$ means the variable with the name of the value of the variable you wrote

    Bit weird to explain in a foreign language so here's an example:

    $color='blue';
    $blue='foo';
    
    echo $$color;
    

    will output

    foo
    

    $k=& $v;
    

    means that $k is a reference to $v

提交回复
热议问题