布尔(英语:Boolean)是计算机科学中的逻辑数据类型。它是只有两种值的原始类型,通常是True和False。
这是最简单的类型。boolean 表达了真值,可以为 TRUE 或 FALSE。某些脚本也会在适当时将值 true 和 false 转换为 1 和 0。
Booleans are used to represent the concepts of true and false.They are most often used for testing if a statement is true or false and they'll play a bigger role when we discuss logical expressions.Note that there is a difference between boolean true/false and the strings "true"/"false".
虽然简单,但是我们还是有必要深入了解一下,先看一下下面的程序:
echo true; echo false;
猜猜程序的输出?
1
输出1和空值。
再来看一个例子:
$var1 = 3; $var2 = "nowamagic"; $var4 = NULL; echo isset($var1); echo isset($var2); echo isset($var3); echo empty($var4);
程序输出:
1 1 1
也就是说,在 PHP 里,直接输出 boolean true 为 1,直接输出 boolean false 为 0。
PHP 支持八种原始类型(type)。
四种标量类型:
两种复合类型:
两种特殊类型:
当转换为 boolean 时,以下值被认为是 FALSE:
所有其它值都被认为是 TRUE(包括任何资源)。
现代魔法 推荐于 2013-02-27 10:23