2.2.4. 布尔类型
Boolean型要注意的,以下值都是false:
-
关键字 false
-
整数 0
-
浮点数 0.0
-
空字符串 ("" ) 和字符串 "0"
-
0个元素的数组
-
没有值的和函数的对象
-
NULL
可以使用is_bool( )来判断一个值是不是布尔类型
if (is_bool($x)) {
// $x is a Boolean
}
2.3.1. 变量的作用域
1.本地的
function update_counter ( ) {
$counter++;
}
$counter = 10;
update_counter( );
echo $counter;
10
2. 全局的
用global声明
function update_counter ( ) {
global $counter;
$counter++;
}
$counter = 10;
update_counter( );
echo $counter;
11
一种更复杂的更新全局变量的方式,使用PHP的$GLOBALS数组:
function update_counter ( ) {
$GLOBALS[counter]++;
}
$counter = 10;
update_counter( );
echo $counter;
11
3. static
function update_counter ( ) {
static $counter = 0;
$counter++;
echo "Static counter is now $counter\n";
}
$counter = 10;
update_counter( );
update_counter( );
echo "Global counter is $counter\n";
Static counter is now 1
Static counter is now 2
Global counter is 10
4. 函数参数
function greet ($name) {
echo "Hello, $name\n";
}
greet("Janet");
Hello, Janet