is_bool()

Description

The is_bool() function is used to determine if a passed variable is a Boolean.

Syntax


bool is_bool( mixed $var )

Parameters

$var
Variable to test.

Return Value

boolean
A Boolean value is returned as follows:

  • true if passed variable is a bool
  • false if passed variable is not a bool

Examples

1. Check All Types with is_bool()

This example creates a variable of each data type and passes it to is_bool(). The return value of the is_bool() call is passed to var_dump() to display its type and value. Note that the file data.txt exists in the directory so the call to fopen() on line 8 succeeds.

<?php
$boolVal = true;
$intVal = 6;
$floatVal = 3.14;
$stringVal = "Hello";
$arrayVal = array( 1, "two", 3.4 );
$objectVal = new stdClass;
$resourceVal = fopen("./data.txt", "r");
$nullVal = null;

var_dump( is_bool( $boolVal ));
var_dump( is_bool( $intVal ));
var_dump( is_bool( $floatVal ));
var_dump( is_bool( $stringVal ));
var_dump( is_bool( $arrayVal ));
var_dump( is_bool( $objectVal ));
var_dump( is_bool( $resourceVal ));
var_dump( is_bool( $nullVal ));

fclose($resourceVal);
?>

The output from the program is shown below. Note that only the $boolVal variable tests true as a bool.

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

See Also

is_array
is_callable
is_double
is_float
is_int
is_integer
is_long
is_null
is_numeric
is_object
is_real
is_resource
is_scalar
is_string
var_dump

[other is_* functions]
var_dump()

References