boolean: Converting to boolean

Converting to boolean

To explicitly convert a value to boolean, use the (bool) or (boolean) casts.

In most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.

Considered FALSE

<?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)
?>
  • The boolean FALSE itself.
  • The integer 0 (zero).
  • The float 0.0 (zero).
  • The empty string, and the string &amp;quot;0&amp;quot;.
  • An array with zero elements.
  • The special type NULL (including unset variables).
  • SimpleXML objects created from empty tags.

Related concepts

boolean: Converting to boolean — Structure map

Clickable & Draggable!

boolean: Converting to boolean — Related pages: