Boolean type
This is the simplest type. Boolean expresses the truth value, which can be TRUE or FALSE.
Note: The Boolean type is introduced in PHP 4.
Grammar
To specify a Boolean value, use the keyword TRUE or FALSE. Two are not case-sensitive.
$foo = True; Assign the value TRUE to $foo
?>
Typically you return a Boolean value with some operators and pass it to the control flow.
= = is an operator that detects whether two variables are equal and returns a Boolean value
if ($action = = "Show_version") {
echo "The version is 1.23";
}
It is not necessary to do so ...
if ($show _separators = = TRUE) {
echo "\ n";
}
// ... Because you can use the following simple way:
if ($show _separators) {
echo "\ n";
}
?>
Convert to a Boolean value
To explicitly convert a value to a Boolean, use either (BOOL) or (Boolean) to cast. However, there are many cases where casting is not required because the value is automatically converted when an operator, function, or process control structure requires a Boolean parameter.
See also type-trick.
When converted to Boolean, the following values are considered FALSE:
The Boolean value FALSE itself the integer value 0 (0) The Float value 0.0 (0) The empty string, and the string "0" does not include any element of an array that does not include any member variables of the object (PHP 4.0 applies only) special type NULL (including the variable that has not been set SimpleXML objects generated from XML documents without any tags (tags)
All other values are considered TRUE (including any resources).
Warning
-1 and other non-0 values (either positive or negative) are considered to be true!
Var_dump ((BOOL) ""); BOOL (FALSE)
Var_dump ((bool) 1); BOOL (TRUE)
Var_dump ((BOOL)-2); BOOL (TRUE)
Var_dump ((bool) "foo"); BOOL (TRUE)
Var_dump ((bool) 2.3e5); BOOL (TRUE)
Var_dump ((BOOL) array (12)); BOOL (TRUE)
Var_dump ((bool) array ()); BOOL (FALSE)
Var_dump ((BOOL) "false"); BOOL (TRUE)
?>