Backend Development 4 min read

Using PHP's is_bool() Function to Check Boolean Types

This article explains how the PHP is_bool() function determines whether a variable is of boolean type, provides its syntax, parameter and return details, includes a complete code example with execution results, and highlights its practical use for type validation in backend development.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's is_bool() Function to Check Boolean Types

In PHP, the is_bool() function checks whether a variable is of boolean type and returns true if it is, otherwise false .

Syntax:

<code>bool is_bool ( mixed $var )</code>

Parameter description:

$var : the variable to be tested.

Return value:

If $var is a boolean, the function returns true ; otherwise it returns false .

Specific code example:

<code>&lt;?php
$var1 = true;
$var2 = false;
$var3 = "true";
$var4 = 1;

// Check if variables are boolean types
if (is_bool($var1)) {
    echo "变量var1是布尔类型";
} else {
    echo "变量var1不是布尔类型";
}

if (is_bool($var2)) {
    echo "变量var2是布尔类型";
} else {
    echo "变量var2不是布尔类型";
}

if (is_bool($var3)) {
    echo "变量var3是布尔类型";
} else {
    echo "变量var3不是布尔类型";
}

if (is_bool($var4)) {
    echo "变量var4是布尔类型";
} else {
    echo "变量var4不是布尔类型";
}
?>
</code>

The execution result of the above code is:

<code>变量var1是布尔类型
变量var2是布尔类型
变量var3不是布尔类型
变量var4不是布尔类型
</code>

In this example, $var1 and $var2 are assigned the boolean values true and false , so is_bool() returns true for both. $var3 holds the string "true", which is not a boolean, so the function returns false . $var4 is an integer 1 , also not a boolean, resulting in false .

By using is_bool() , developers can easily verify a variable's type, which is especially useful for validating user input to ensure data accuracy and security in backend applications.

PHP learning recommendations:

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Master Course

Workerman+TP6 Real‑time Chat System – Limited Time Offer

backendphpBoolean()type checkingphp-functionsis_bool
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.