php

How to check if a variable is a Boolean in PHP?

Boolean variables contain true or false values. To find out if any variable contains a Boolean value, different approaches can be used:

  • is_bool($var): The function is_bool() checks whether a variable’s type is Boolean and returns true or false.
  • Comparison using “===” or ($var===true || $var===false): If you compare the variable with the operator “===” with true and false and if it is identical to one of the values, then it is a boolean value. Since the functionality of is_bool($var) is same, the function should be preferred.
  • Comparison using “==” or ($var == true || $var == false): If you use “==” as an operator instead, the test is more tolerant. For example, 0(integer) or “”(empty string) would also result in a Boolean value.
  • gettype($var): If you apply gettype() to a variable, you will get the datatype of the variable as a string. This can be evaluated. In case of boolean variables it must have the value “boolean”.

The recommended way to check the variables is to use is_bool(). It is readable for other developers and is performant at the same time.
 

PHP MCQ - Multiple Choice Questions and AnswersPHP Questions and Answers – Object-Oriented OOP – Part 1This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Object-Oriented OOP”.   1. The concept of…Read More
Example 1: is_bool($var)
<?php
    function myFunction($bool) 
    {
        if (!is_bool($bool)) 
        {
            throw new Exception('Function myFunction expects a Boolean value.');
        }
        echo("myFunction called successfully.\n");
    }
 
    try 
    {
        myFunction(false);
        myFunction(true);
        myFunction(0);
    } 
    catch (Exception $e) 
    {
        echo('Error with message "'.$e->getMessage().'" in line '.$e->getLine().'.');
    }
?>

Output:

myFunction called successfully.
myFunction called successfully.
Error with message "Function myFunction expects a Boolean value." in line 6.
What is the difference between == and === in PHPWhat is the difference between == and === in PHPIn this tutorial, we’re going to see two ways to compare variables for equality in PHP.   The general behavior of “==” In most programming…Read More
 

Example 2: Comparison using “===”
<?php
    function myFunction($bool) 
    {
        if ($bool!==true && $bool!==false)
        {
            throw new Exception('Function myFunction expects a Boolean value.');
        }
        echo("myFunction called successfully.\n");
    }
 
    try 
    {
        myFunction(false);
        myFunction(true);
        myFunction(0);
    } 
    catch (Exception $e) 
    {
        echo('Error with message "'.$e->getMessage().'" in line '.$e->getLine().'.');
    }
?>

Output:

myFunction called successfully.
myFunction called successfully.
Error with message "Function myFunction expects a Boolean value." in line 6.

 

Example 3: Comparison using “==”
<?php
    function myFunction($bool) 
    {
        if ($bool!=true && $bool!=false)
        {
            throw new Exception('Function myFunction expects a Boolean value.');
        }
        echo("myFunction called successfully.\n");
    }
 
    try 
    {
        myFunction(false);
        myFunction(true);
        myFunction(0);
    } 
    catch (Exception $e) 
    {
        echo('Error with message "'.$e->getMessage().'" in line '.$e->getLine().'.');
    }
?>

Output:

myFunction called successfully.
myFunction called successfully.
myFunction called successfully.
How to check if a variable is a String in PHPHow to check if a variable is a String in PHP?Which methods are available in PHP to check if a variable is a String? In this tutorial, we are going to see how to check…Read More
Example 4: Using gettype()
<?php
    function myFunction($bool) 
    {
        if (gettype($bool) !== 'boolean')
        {
            throw new Exception('Function myFunction expects a Boolean value.');
        }
        echo("myFunction called successfully.\n");
    }
 
    try 
    {
        myFunction(false);
        myFunction(true);
        myFunction(0);
    } 
    catch (Exception $e) 
    {
        echo('Error with message "'.$e->getMessage().'" in line '.$e->getLine().'.');
    }
?>

Output:

myFunction called successfully.
myFunction called successfully.
Error with message "Function myFunction expects a Boolean value." in line 6.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *