Grammar:
To specify a Boolean value, use the keyword TRUE or FALSE. Two are not case-sensitive.
The code is as follows |
Copy Code |
<?php $foo = True; Assign the value TRUE to $foo ?> |
The Boolean value returned by the usual operator is passed to the control flow.
The code is as follows |
Copy Code |
<?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 Boolean value
To explicitly convert a value to a Boolean, use (BOOL) or (Boolean) to cast. In many cases, however, a cast is not required because the value is automatically converted when the operator, function, or process control structure requires a Boolean parameter.
When converted to Boolean, the following values are considered FALSE:
1. Boolean value FALSE itself
2. Integer value 0 (0)
3. Floating-point value 0.0 (0)
4. Empty string, and string "0"
5. An array that does not include any elements
6. Objects that do not include any member variables (PHP 4.0 applies only)
7. Special type NULL (including variables that have not been assigned)
8. SimpleXML object generated from the empty tag
All other values are considered TRUE (including any resources).
Note: 1 and other non-0 values (both positive and negative) are considered TRUE!
code is as follows |
copy code |
<?php 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); //bool (TRUE) Var_dump ((BOOL) array ()); &nbs p; //bool (FALSE) Var_dump ((bool) "false"); //bool (TRUE) ?> |