PHP Boolean type
This is the simplest type. Boolean represents the TRUE value, which can be TRUE or FALSE.
Note: The Boolean type is introduced in PHP 4.
Syntax
To specify a Boolean value, use the keyword TRUE or FALSE. Both are case insensitive.
$ Foo = True; // assign the value TRUE to $ foo
?>
You usually use some operators to return a boolean value and pass it to the control process.
// = Is an operator that checks whether two variables are equal and returns a boolean value.
If ($ action = "show_version "){
Echo "The version is 1.23 ";
}
// This is not necessary...
If ($ show_separators = TRUE ){
Echo "\ n ";
}
//... Because the following simple method can be used:
If ($ show_separators ){
Echo "\ n ";
}
?>
Convert to a Boolean value
To convert a value to a boolean value explicitly, use (bool) or (boolean) to forcibly convert the value. However, in many cases, forced conversion is not required, because this value is automatically converted when the operator, function, or flow control structure requires a boolean parameter.
See type tricks.
When converted to boolean, the following values are considered FALSE:
The Boolean value FALSE itself the integer value 0 (0) the floating point value 0.0 (0) null string, and the string "0" does not include any element array does not include any member variable object (applicable only in PHP 4.0) Special type NULL (including unset variables) simpleXML object generated from an XML document without any tags
All other values are considered to be TRUE (including any resources ).
Warning
-1 is considered TRUE like other non-zero values (both positive and negative values!
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)
?>