Backend Development 4 min read

Using PHP is_bool() to Check Boolean Variables

This article explains how the PHP is_bool() function can be used to determine whether a variable holds a boolean value, provides clear code examples with different variable types, and shows the resulting output to illustrate correct usage.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP is_bool() to Check Boolean Variables

In PHP programming, it is often necessary to verify whether a variable is a boolean. PHP provides the built‑in is_bool() function for this purpose.

The is_bool() function returns true if the supplied variable is of boolean type, otherwise it returns false .

Below is a practical example that defines four variables with different types and uses is_bool() to check each one:

The script creates $var1 (boolean true), $var2 (boolean false), $var3 (integer 1), and $var4 (string "true"). It then checks each variable with is_bool() and prints whether the variable is a boolean.

Running the code produces the following output:

变量 $var1 是布尔值
变量 $var2 是布尔值
变量 $var3 不是布尔值
变量 $var4 不是布尔值

From the results, $var1 and $var2 are identified as booleans, while $var3 and $var4 are not, demonstrating that is_bool() correctly distinguishes true boolean values from other types such as integers and strings.

In summary, the is_bool() function is a useful tool for quickly checking variable types in PHP, helping developers write more reliable and type‑safe code.

BackendPHPTutorialBooleanfunctionis_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.