global作用域
1 global在函数内部
$somevar=15; function addit () { GLOBAL $somevar; $somevar++ ; echo "somevar is $somevar"; } addit ()
输出结果:
somevar is 16
2 不存在global
$somevar=15; function addit () { $somevar; $somevar++ ; echo "somevar is $somevar"; } addit ()
输出结果:
somevar is 1
3 global 在外面
global $somevar; $somevar=15; function addit () { $somevar++ ; echo "somevar is $somevar"; } addit ()
输出结果:
somevar is 1
4 函数内部 删除掉 $somevar++ ;
global $somevar; $somevar=15; function addit () { echo "somevar is $somevar"; } addit ()
输出结果:
somevar is
总结 :
global函数内部定义变量,则为全局变量,如果在外部定义变量,则对函数内部不起作用。