PHP is_bool() Function

PHP

PHP is_bool() - Check Boolean

The is_bool() function in PHP is a simple yet powerful tool used to determine whether a variable is of boolean data type. Booleans are fundamental in programming as they represent true or false values that control the flow of logic. In this tutorial, you will learn how to use is_bool(), see practical examples, best practices, common pitfalls, and even prepare for interview questions around it.

Prerequisites

  • Basic understanding of PHP syntax.
  • Familiarity with PHP variable types.
  • A working installation of PHP 5.0+ (as is_bool() is available in all modern PHP versions).

Setup

To try the is_bool() function, ensure you have a PHP environment ready. You can use:

  • XAMPP / MAMP / WAMP local servers
  • Command-line PHP interpreter
  • Online PHP sandboxes (e.g., PHPFiddle, 3v4l.org)

What is PHP is_bool() Function?

The is_bool() function checks a variable and returns true if the variable is of boolean type (true or false). Otherwise, it returns false.

Syntax:

bool is_bool ( mixed $var )

Parameters:

  • $var: The variable to test.

Return Value: Returns true if $var is a boolean, else false.

Examples Explained

Example 1: Basic Usage

<?php
$var1 = true;
$var2 = false;
$var3 = 0;
$var4 = "true";

var_dump(is_bool($var1)); // bool(true)
var_dump(is_bool($var2)); // bool(true)
var_dump(is_bool($var3)); // bool(false)
var_dump(is_bool($var4)); // bool(false)
?>

Explanation: Only $var1 and $var2 are boolean types. The integer 0 and string "true" are not booleans, so is_bool() returns false.

Example 2: Validating Function Return Values

<?php
function checkFlag() {
    // returns a boolean flag
    return true;
}

$result = checkFlag();
if (is_bool($result)) {
    echo "Result is boolean.";
} else {
    echo "Result is NOT boolean.";
}
?>

This example shows how you can use is_bool() to validate that a function returns a boolean value, which is useful when handling logical flags or status checks.

Example 3: Using is_bool() with Mixed Data

<?php
$data = [true, false, 1, '', null, 'false', 0];

foreach ($data as $item) {
    if (is_bool($item)) {
        echo var_export($item, true) . " is boolean.\n";
    } else {
        echo var_export($item, true) . " is NOT boolean.\n";
    }
}
?>

This loops through an array of mixed values and identifies which ones are boolean types.

Best Practices

  • Use is_bool() to explicitly check the type when your logic depends strictly on boolean values.
  • Do not confuse boolean values with truthy/falsy values. Integers like 0, empty strings, or null can behave like false in conditions, but are not boolean.
  • When validating user input or external data, consider is_bool() to ensure type integrity before logic execution.
  • Combine is_bool() with strict type checking in functions or method declarations in PHP 7+ for robustness.

Common Mistakes to Avoid

  • Using is_bool() to check for truthy/falsy instead of exact boolean type: remember is_bool(0) returns false.
  • Assuming strings like "true" or "false" are boolean β€” these are strings, not boolean types.
  • Not considering that PHP variables can change type dynamically; check with is_bool() before strictly relying on boolean behavior.

Interview Questions and Answers

Junior Level

  • Q1: What does is_bool() function in PHP do?
    A: It checks if a variable is of boolean type and returns true if it is; otherwise, false.
  • Q2: What will is_bool(true) return?
    A: It will return true because the variable is a boolean.
  • Q3: Will is_bool(1) return true?
    A: No, because 1 is an integer, not a boolean type.
  • Q4: Does is_bool() consider "false" (string) as boolean?
    A: No, it only returns true if the value itself is boolean type.
  • Q5: How can is_bool() help in programming logic?
    A: It ensures that the variable used in logical conditions is strictly a boolean for predictable behavior.

Mid-Level

  • Q1: How does is_bool() differ from loose type checking in PHP?
    A: is_bool() checks the exact boolean data type, while loose type checking may treat other values as boolean-like.
  • Q2: Can is_bool() be used for type safety in functions?
    A: Yes, it can validate that inputs or outputs are true boolean values enhancing type safety.
  • Q3: What will be the output of var_dump(is_bool(false));
    A: It will output bool(true) because false is a boolean.
  • Q4: Is is_bool() affected by PHP type juggling?
    A: No, is_bool() strictly checks the type without conversion.
  • Q5: How do you differentiate between is_bool() and is_scalar() for boolean checking?
    A: is_bool() checks only for booleans, is_scalar() checks if var is a scalar (boolean, int, float, string).

Senior Level

  • Q1: How would you enforce boolean return types in legacy code without scalar type hinting but relying on is_bool()?
    A: Use is_bool() checks after function calls to validate return values and handle errors or cast accordingly.
  • Q2: In scenarios requiring strict boolean validation, how does is_bool() complement type declarations introduced in PHP 7?
    A: is_bool() can be used for runtime validation where type declarations are not available or for legacy compatibility.
  • Q3: Can is_bool() be used for performance-critical checks? Why or why not?
    A: It is lightweight and performant, suitable for critical checks where precise boolean type detection is necessary.
  • Q4: How does is_bool() interact with PHP objects implementing __toString method returning "true" or "false" strings?
    A: It will return false because the object itself is not a boolean type, regardless of string output from __toString().
  • Q5: Can you design a custom function that extends is_bool() functionality to check if a variable is either boolean or a string with "true"/"false"? Provide an outline.
    A: Yes, write a function that returns true if is_bool($var) returns true or if is_string($var) and its lowercase value is "true" or "false".

FAQ

Q: Does is_bool() convert values to boolean before checking?
A: No. is_bool() only checks if the variable is *already* of boolean type without any conversion.
Q: What types return true when passed to is_bool()?
A: Only true and false (boolean values) return true.
Q: Is is_bool() case sensitive?
A: The function itself is case-insensitive, but it doesn’t apply here since it checks the type, not string values.
Q: Can is_bool() be used to check variables in arrays?
A: Yes, you can iterate over arrays and use is_bool() on each element.
Q: Does is_bool() detect boolean objects or only primitives?
A: PHP does not have boolean objects; is_bool() only detects primitive boolean values.

Conclusion

The is_bool() function is a fundamental PHP tool for validating the boolean nature of variables, crucial for robust, predictable logic handling. Whether you are validating inputs, checking function return types, or debugging complex conditions, understanding and correctly using is_bool() enhances the quality of your PHP code. Use this function to avoid common pitfalls related to type juggling and improve your applications' reliability.