PHP Boolean data types and process-controlled data type conversions
The Boolean is the simplest type of data. can be true or false, and the keyword is not case-sensitive.
To explicitly convert a value to a Boolean type, use (bool) or (Boolean) to cast. In many cases, PHP will perform automatic type conversions, especially when the conditional expression of a Process Control statement is evaluated.
When converted to a Boolean value, the following values are considered false:
1) boolean value false;
2) integer value 0 (0);
3) floating-point value 0.0 (0);
4) Blank string and string "0";
5) No array of member variables;
6) object without unit (only applicable to PHP4);
7) Special type null (including variables not yet set);
In addition, all other values are considered true (including any resources).
The following program examples can be used to test:
During the development process, when the condition is judged, it is important to pay attention to the recessive danger of the Boolean type default conversion.
The following topics are in this regard:
"; if (" 0 ") echo" true "; Elseecho" False-2 "."
"; if ($i) echo" true "; Elseecho" false-3 "."
"; if (" false ") echo" true "; Elseecho" false-4 "."
"; if (false) echo" true "; Elseecho" false-5 "."
"; if (0.0) echo" true "; Elseecho" false-6 "."
"; if (PHP) echo" true "; Elseecho" false-7 "."
"; if (") echo "true"; Elseecho "False-8". "
"; if (null) echo" true "; Elseecho" false-9 "."
"; if (array) echo" true "; Elseecho" false-10 "."
";" if (Array ()) echo "true"; Elseecho "false-11". "
";/* Output: False-1false-2false-3true false-5false-6true false-8false-9true FALSE-11 Analysis: If statement returns false when the condition is not satisfied (that is, the conditional expression evaluates to false). PHP If statement does not have then, there is elseif (same else if) when converted to Boolean, the following values are considered False:1) boolean value false;2) integer value 0 (0); 3) floating-point value 0.0 (0); 4) blank string and string "0" ; 5) No array of member variables, 6) object with no cell (only for PHP4), 7) special type null (including variable not yet set);*/?>