echo gettype($bool); // prints out "boolean"
echo gettype($str); // prints out "string"
// If this is an integer, increment it by four
if (is_int($int)) {
$int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($bool)) {
echo "String: $bool";
}
?
>
如果你要将一个变量强制转换为某类型,可以对其使用强制转换或者 settype() 函
数。
注意变量根据其当时的类型在特定场合下会表现出不同的值。更多信息见类型戏法。
此外,你还可以参考 PHP 类型比较表看不同类型相互比较的例子。
布尔型
这是最简单的类型。boolean
表达了真值,可以为 TRUE
或 FALSE。
注:
布尔类型是 PHP 4 引进的。
语法
要指定一个布尔值,使用关键字 TRUE
或 FALSE。两个都是大小写不敏感的。
<?php
$foo = True; // assign the value TRUE to $foo