先看下面的代碼:
<?php
$var1 = “#####”;
$var2 = “&&&&&”;
function global_references($use_globals) {
global $var1, $var2;
if (!$use_globals) {
$var2 =&$var1; //1
} else {
$GLOBALS[“var2”] =&$var1; //2
}
}
global_references(false);
echo “var2 is set to $var2<br/>”;
global_references(true);
echo “var2 is set to $var2<br/>”;
?>
輸出的結果如下:
var2 is set to &&&&&
var2 is set to #####
可見,上面的代碼中:
$var2 =&$var1; //1
隻對函數內部可見。
而
$GLOBALS[“var2”] =&$var1; //2
在全局范圍內可見。