Backend Development 3 min read

Using PHP is_bool() to Determine Boolean Types

This article explains PHP's is_bool() function, its syntax, parameters, return values, and provides a complete code example showing how to check various variables for boolean type and interpret the results in practical development scenarios.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP is_bool() to Determine Boolean Types

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

Syntax:

bool is_bool ( mixed $var )

Parameter: $var – the variable to be tested.

Return value: true if $var is boolean, false otherwise.

Example code:

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

// Check each variable
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 output will be:

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

The example demonstrates that literals true and false are recognized as booleans, while a string "true" and integer 1 are not, illustrating how is_bool() can be used for type validation in real‑world PHP applications.

backendPHPTutorialBooleantype checkingis_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.