PHP语言中global和$GLOBALS[]的分析
php中global和$GLOBALS不仅仅是写法不一样以为,2者的区别还是很大的,在实际应用中需要注意!
先看下面的例子:
<?php // 例子1 function test_global() { global $var1, $var2; $var2 =& $var1; } function test_globals() { $GLOBALS['var3'] =& $GLOBALS['var1']; } $var1 = 5; $var2 = $var3 = 0; test_global(); print $var2 .”\n”; test_globals(); print $var3 .”\n”; ?>
<?php // 例子2 function test() { global $a; unset($a); } $a = 1; test(); print $a; ?>
<?php function myfunction(){ global $bar; unset($bar); } $bar=”someting”; myfunction(); echo $bar; ?>
<?php function foo() { unset($GLOBALS['bar']); } $bar = “something”; foo(); echo $bar; ?>
<?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?>