Details of boolean of Boolean type in php

  • 2020-10-23 20:54:23
  • OfStack

When converted to boolean, the following values are considered FALSE:

the Boolean value FALSE itself

the integer value 0 (zero)

the floating point value 0.0 (zero)

Empty string, and the string "0"

An array that does not contain any elements

Objects that do not include any member variables (PHP 4.0 only)

Special type NULL (including variables not yet set)

An SimpleXML object generated from an XML document without any markup (tags)

All other values are considered TRUE (including any resources).

Warning
-1, like any other non-zero value (plus or minus), is considered TRUE!


<?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(12)); // bool(true)
var_dump((bool) array());   // bool(false)
var_dump((bool) "false");   // bool(true)  

When Boolean rotates a string, true= > false = 1 > "";


Related articles: