PHP Function Reference

PHP is_bool() Function



The PHP is_bool() function checks whether a variable is a boolean value. The function returns true if the variable is a boolean value, otherwise it returns false.

Syntax

is_bool(variable)

Parameters

variable Required. Specify the variable being evaluated.

Return Value

Returns true if variable is a boolean value, false otherwise.

Example:

The example below shows the usage of is_bool() function.

<?php
var_dump(is_bool(true));   //returns: bool(true)
var_dump(is_bool(false));  //returns: bool(true)
var_dump(is_bool(FALSE));  //returns: bool(true)
var_dump(is_bool(TRUE));   //returns: bool(true)
var_dump(is_bool(10>5));   //returns: bool(true)

echo "\n";

var_dump(is_bool(10));       //returns: bool(false)
var_dump(is_bool(10.5));     //returns: bool(false)
var_dump(is_bool(1e5));      //returns: bool(false)
var_dump(is_bool('xyz'));    //returns: bool(false)
var_dump(is_bool(null));     //returns: bool(false)
var_dump(is_bool("false"));  //returns: bool(false)
var_dump(is_bool(array()));  //returns: bool(false)
?>

The output of the above code will be:

bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)

Example:

Consider the example below where this function is used to check all elements of an array if they are a boolean value or not.

<?php
$Arr = array(10, 10.5, null, false, true, "xyz", 
             1e3, "false", 10>5, array()); 

foreach ($Arr as $value) {
  echo "is_bool(".var_export($value, true).") = ";
  var_dump(is_bool($value));
}
?>

The output of the above code will be:

is_bool(10) = bool(false)
is_bool(10.5) = bool(false)
is_bool(NULL) = bool(false)
is_bool(false) = bool(true)
is_bool(true) = bool(true)
is_bool('xyz') = bool(false)
is_bool(1000.0) = bool(false)
is_bool('false') = bool(false)
is_bool(true) = bool(true)
is_bool(array (
)) = bool(false)

Example:

Consider one more example where this function is used with control statements.

<?php
$x = false;
$y = 0;

//$x is a boolean, it will return true
if (is_bool($x) === true) 
  echo "Yes, this is a boolean.";

echo "\n";

//$y is not a boolean, it will return false
if (is_bool($y) === false) 
  echo "No, this is not a boolean.";
?>

The output of the above code will be:

Yes, this is a boolean.
No, this is not a boolean.

❮ PHP Variable Handling Reference