PHP is_bool() Function

What is PHP is_bool() Function?

If you want to know whether a variable’s type is exactly boolean type or not, use is_bool() function.  there are only two Boolean values – TRUE and FALSE.

Syntax:

is_bool(variable)

Parameters:

The Function has 1 parameter which is required-

variable (Required): It specifies a variable.

Return Values:

The function returns-

  • TRUE, if the variable is a boolean type variable
  • FALSE, if the variable is not a boolean type variable

Examples:

Example 1:

<?php
if (is_bool(TRUE) === TRUE) {
    echo "Yes, this is a boolean.";
}else {
    echo "No, this is not a boolean.";
}
?>

Output:

Yes, this is a boolean.

Example 2:

<?php
if (is_bool(1) === TRUE) {
    echo "Yes, this is a boolean.";
}else {
    echo "No, this is not a boolean.";
}
?>

Output:

No, this is not a boolean.

Example 3:

<?php
if (is_bool("TRUE") === TRUE) {
    echo "Yes, this is a boolean.";
}else {
    echo "No, this is not a boolean.";
}
?>

Output:

No, this is not a boolean.

Notes on is_bool() Function:

  • The is_bool() function doesn’t consider “falsy” and “truthy” values as Boolean values. “Truthy” / “FALSY” values are those that are not strictly the boolean but are treated so. Truthy example: 1, “1”, “true”etc. Falsy example: 0, “FALSE” etc.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_bool() Function

is_bool() is a built-in variable handling function. to know exactly whether a variable is booleaan or not, use this function.

Reference:

https://www.php.net/manual/en/function.is-bool.php