You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
638 B
26 lines
638 B
<?php
|
|
/**
|
|
* php的全局变量到函数里不能用,在函数里需要关键字global引用
|
|
*/
|
|
|
|
|
|
$a=1;
|
|
$b=3;
|
|
|
|
function func()
|
|
{
|
|
global $a; //使用global引入全局变量
|
|
echo "a:$a".PHP_EOL; //func内使用global引用了a和b,所以c没有打印出来
|
|
echo "b:$b".PHP_EOL;
|
|
}
|
|
|
|
//$GLOBALS['var']
|
|
function func2()
|
|
{
|
|
echo "a:".$GLOBALS['a'].PHP_EOL; // $GLOBALS['var'] 指的是函数外部变量本身,是一个变量.在程序的任意位置都能使用
|
|
echo "b:".$GLOBALS['b'].PHP_EOL;
|
|
}
|
|
|
|
func();
|
|
func2();
|
|
?>
|