PHP中global,$GLOBALS的比较

来源:互联网 发布:fda医疗器械数据库 编辑:程序博客网 时间:2024/05/16 01:22
一直对这两个变量比较迷惑,今天写了一个程序,测试了一下,总算明白了。

    代码:

<?php$GLOBALS['test'] = "i am valid.";

function testFun1(){ echo "i am in testFun1().<br/>";

    echo '$GLOBALS is '.$GLOBALS['test'].'<br/>';

 unset($GLOBALS['test']); echo '$GLOBALS is '.$GLOBALS['test'].'<br/>';}

function testFun2(){ echo "i am in testFun2().".'<br/>';    global $test;

 $test = "changed";  echo '$GLOBALS is '.$GLOBALS['test'].'<br/>';

 unset($test);

 echo "global test is ".$test.'<br/>' echo '$GLOBALS is '.$GLOBALS['test'].'<br/>';}

testFun1();testFun2();?>

结果:

i am in testFun1().$GLOBALS is i am valid.$GLOBALS is i am in testFun2().$GLOBALS is changedglobal test is $GLOBALS is changed

引用他人的一句话:

也就是说global $var其实就是$var = &$GLOBALS['var']。调用外部变量的一个别名而已

原创粉丝点击