An introduction to the Boolean type of PHP data types

  • 2020-06-01 08:37:20
  • OfStack

A Boolean type is the simplest type in PHP. Its value can be TRUE or FALSE.

Such as:

$foo=false;
$foo1=true;
When echo "is false, the output value is :".$foo; // no output value
echo " < br / > Is true, the output value is :".$foo1; / / output 1

Here are the main details:

When converted to boolean, the following values are considered to be FALSE:
1. the Boolean FALSE itself
2. the integer value 0 (zero)
3, the floating point value 0.0 (zero) empty string, and the string "0"
4. An array that does not contain any elements
5. Objects excluding any member variables (PHP 4.0 only)
6. Special type NULL (including variables not yet set)
7. SimpleXML objects generated from XML documents without any markup (tags)

//$a=0;
//$a=0.0;
$a="0";
var_dump((bool) 0);
echo " < br / > ";
var_dump((bool) array());
if($a==false){
echo "null 0 is converted to false by default, successful!" ;
}else{
echo "cannot be converted to false";
}

Output:

bool(false)
bool(false) null 0 is converted to false by default, successful!


Related articles: