This is the simplest type. Boolean expresses the truth value, which can be true or false.
To specify a Boolean value, use the keyword True or false. Two are not case-sensitive.
<?php
Header("Content-type:text/html;charset=utf-8");
$foo=True;//assign the value TRUE to $foo
?>
The Boolean result returned by the usual operator is passed to the control flow.
<?php
//= = 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"}
// ... Because you can use the following simple way:
if($show _separators) {
Echo"}
?>
Convert to a Boolean value
to explicitly convert a value to Boolean, with (bool) or (Boolean) to cast. However, in many cases there is no need to cast, because when an operator, function, or process control structure requires a Boolean parameter, the change is automatically converted.
When converted to Boolean, the value is considered false:
1. The Boolean value false itself
2. integer value 0
3. floating-point value 0.0
4. An empty string, as well as a string " 0 "
5. an array that does not include any elements
6. objects that do not include any member variables
7. Special type null
8. SimpleXML object generated from an empty tag
Note:-1 and other non-0 values (either positive or negative) are considered true
<?php
Var_dump((BOOL)false);//bool (FALSE)
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)
?>
Boolean Boolean type