<?php
function test() {
$c = 2;
}
$a = 1;
$b = 2;
?>
上边的代码的$a和$b都在$GLOBALS里,与$GLOBALS[\'a\']和$GLOBALS[\'B\']是等价的,但$c 不是,他是函数的私有变量,不在$GLOBALS里,如下可知:
有趣的是:$GLOBALS变量又是$GLOBALS变量的值,你会发现$GLOBALS
下有$GLOBALS,这个$GLOGALS下又有$GLOBALS,这样一直层层不断的套下去,很是有趣。function test() {
$c = 2;
}
$a = 1;
$b = 2;
?>
上边的代码的$a和$b都在$GLOBALS里,与$GLOBALS[\'a\']和$GLOBALS[\'B\']是等价的,但$c 不是,他是函数的私有变量,不在$GLOBALS里,如下可知:
<?php
// 例子2
function test() {
$c = 2;
$GLOBALS[\'a\'] = \'DDD\';
}
$a = 1;
$b = 2;
test();
print $a;//此时输出的是DDD而不是1.
?>
// 例子2
function test() {
$c = 2;
$GLOBALS[\'a\'] = \'DDD\';
}
$a = 1;
$b = 2;
test();
print $a;//此时输出的是DDD而不是1.
?>
<?php
// 例子3
function test() {
$c = 2;
$GLOBALS[\'a\'] = \'DDD\';
}
$a = 1;
$b = 2;
test();
print_r($GLOBALS[\'GLOBALS\'][\'GLOBALS\'][\'GLOBALS\'][\'GLOBALS\']);
print_r($GLOBALS);
//以上两个打印的结果是一样的
?>
// 例子3
function test() {
$c = 2;
$GLOBALS[\'a\'] = \'DDD\';
}
$a = 1;
$b = 2;
test();
print_r($GLOBALS[\'GLOBALS\'][\'GLOBALS\'][\'GLOBALS\'][\'GLOBALS\']);
print_r($GLOBALS);
//以上两个打印的结果是一样的
?>
文章来源:http://www.phpweblog.net/fuyongjie/archive/2009/02/27/6354.html