Booleans in php

A Boolean value represents a “truth value”—it says whether something is true or not. Like most programming languages, PHP defines some values as true and others as false. Truth and falseness determine the outcome of conditional code such as:

if ($alive) { … }

In PHP, the following values all evaluate to false:

  • The keyword false
  • The integer 0
  • The floating-point value 0.0
  • The empty string (“”) and the string “0”
  • An array with zero elements
  • An object with no values or functions
  • The NULL value

PHP provides true and false keywords for clarity:

$x = 5; // $x has a true value
$x = true; // clearer way to write it
$y = “”; // $y has a false value
$y = false; // clearer way to write it

Use the is_bool() function to test whether a value is a Boolean:

if (is_bool($x)) {
// $x is a Boolean

Leave a Comment