PHP - Boolean

php

How can we use a boolean?

$foo = True;

How can we cast a variable to a boolean?

To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, 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.

What are considered as false?

  1. the boolean FALSE itself
  2. the integer 0 (zero)
  3. the float 0.0 (zero)
  4. the empty string, and the string "0"
  5. an array with zero elements
  6. an object with zero member variables (PHP 4 only)
  7. the special type NULL (including unset variables)
  8. SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource).

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License