Current Location: Home> Latest Articles> Difference Between is_bool and gettype: Why You Shouldn’t Use gettype to Check for Boolean Values

Difference Between is_bool and gettype: Why You Shouldn’t Use gettype to Check for Boolean Values

gitbox 2025-08-26

In PHP, is_bool and gettype are two commonly used functions for checking variable types. Although both can be used for type checking, they behave differently and have distinct purposes, particularly when it comes to boolean values. This article delves into the differences between is_bool and gettype and explains why you shouldn’t rely solely on gettype to check for boolean values.

1. Introduction to is_bool

is_bool is a function specifically designed to determine whether a variable is a boolean. Boolean values can be either true or false. The function returns a boolean value: true if the variable is a boolean, and false otherwise.

Example:

<span>$var1 = <span class="hljs-literal">true</span>;
$var2 = <span class="hljs-number">1</span>;
<p>echo is_bool($var1); <span class="hljs-comment">// Output: 1 (true)<br>
echo is_bool($var2); // Output:  (false)

In this example, is_bool($var1) returns true because $var1 holds the boolean value true. Meanwhile, is_bool($var2) returns false, even though $var2 is 1, because it is not a boolean.

2. Introduction to gettype

gettype is a function that returns the type of a variable as a string. Unlike is_bool, gettype provides a description for all variable types, not just booleans.

Example:

$var1 = true;
$var2 = 1;
<p>echo gettype($var1); // Output: boolean<br>
echo gettype($var2); // Output: integer

Here, gettype($var1) returns boolean, indicating that $var1 is a boolean. gettype($var2) returns integer, indicating that $var2 is an integer.

3. Differences Between is_bool and gettype

Although these two functions may seem to serve similar purposes, there are key differences in their use:

  • Specificity: is_bool is dedicated to checking whether a variable is a boolean, whereas gettype is a general-purpose function that returns types such as boolean, integer, string, and others.

  • Return value: is_bool returns a boolean (true or false), while gettype returns a string describing the variable’s type.

  • Type matching: is_bool only returns true if the variable is strictly true or false. gettype returns a string like boolean and does not differentiate between true and false.

4. Why You Shouldn’t Use gettype to Check for Booleans

gettype returns the variable’s type as a string (e.g., boolean) rather than the actual boolean value. If you use gettype to check for booleans, both true and false are simply categorized as boolean, making it impossible to distinguish them. This means you can only tell if a variable is a boolean, not whether it is specifically true or false.

Example:

$var1 = true;
$var2 = false;
$var3 = 1;
<p>echo gettype($var1); // Output: boolean<br>
echo gettype($var2); // Output: boolean<br>
echo gettype($var3); // Output: integer

Even though $var1 and $var2 are both booleans, gettype only returns boolean and cannot distinguish between true and false. To make this distinction, you need to use a more specific method.

5. Correct Usage of is_bool and gettype

To accurately check if a variable is a boolean, the best approach is to use is_bool. If you need to determine the exact boolean value (true or false), you can compare the variable directly.

Example:

$var1 = true;
$var2 = false;
<p>if (is_bool($var1)) {<br>
echo "var1 is a boolean\n";<br>
}</p>
<p>if ($var1 === true) {<br>
echo "var1 is true\n";<br>
}</p>
<p>if ($var2 === false) {<br>
echo "var2 is false\n";<br>
}

In this example, is_bool first checks if the variable is a boolean. Then, the strict comparison operator === is used to determine whether it is true or false.

6. Conclusion

is_bool and gettype are both useful tools, but they serve different purposes. is_bool is specifically for checking if a variable is a boolean, while gettype provides general type information. Using gettype alone is inaccurate for boolean checks because it cannot distinguish between true and false. Therefore, when checking for boolean types, it is recommended to use is_bool along with direct comparisons to determine the specific value.