📜  PHP | is_bool()(1)

📅  最后修改于: 2023-12-03 14:45:18.098000             🧑  作者: Mango

PHP | is_bool()

The PHP function is_bool() is used to determine whether a variable is a boolean or not. It will return true if the given variable is a boolean and false otherwise.

Syntax
bool is_bool ( mixed $var )

Here, $var is the variable to be checked.

Return Values

If the given variable is a boolean, is_bool() will return true. Otherwise, it will return false.

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

var_dump(is_bool($var1)); // true
var_dump(is_bool($var2)); // true
var_dump(is_bool($var3)); // false
var_dump(is_bool($var4)); // false
?>

In the above example, $var1 and $var2 are both boolean values, so is_bool() returns true for both of them. $var3 is not a boolean value, but rather an integer, so is_bool() returns false for it. Finally, $var4 is a string representation of a boolean value, but it is not a boolean itself, so is_bool() returns false for it.

Conclusion

is_bool() is a simple yet useful function that can be used to quickly determine whether a given variable is a boolean or not. This can be particularly helpful when working with complex data structures where the data types of the different elements may not be immediately apparent.