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:

bool is_bool ( mixed $var )

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:

<?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不是布尔类型";
}
?>

The execution result of the above code is:

变量var1是布尔类型
变量var2是布尔类型
变量var3不是布尔类型
变量var4不是布尔类型

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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

booleantype 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

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.