Table of Content
You can assign the boolean data type to PHP variables or use it as a direct value. This enables you to represent the truth value. It is something either true or false. On other words, the boolean type is similar to the answer to a question with yes or no.
Additionally, we can use this type to handle any two choices during the program stack. In the next section, you will learn how to write boolean type values in various ways.
Boolean Type is Case Insensitive
The boolean type in PHP is case-insensitive and enables you to display the values of boolean ‘true’ or ‘false’ in various cases such as uppercase or lowercase without changing their meaning or functionality.
So, “true or false” is similar to “TRUE and FALSE” or “True and False”. For example:
$bool = True; // Assign the value TRUE to $boolThere is a built-in callback in PHP to check if the current variable has a boolean data type or not. Let’s take a look at it in the section below.
Checking for Boolean Values
PHP has variety of built-in functions for detecting specific data types, such as is_bool function. The is_bool function determines whether a given variable is of the boolean data type. So if the value is not Boolean it will show “false”.
Here is an example:
<?php
$variable1 = true;
$variable2 = 42; // Not a boolean
$variable3 = "false"; // Not a boolean
// Check if $variable1 is a boolean
if (is_bool($variable1)) {
echo '$variable1 is a boolean.';
} else {
echo '$variable1 is not a boolean.';
}
// Check if $variable2 is a boolean
if (is_bool($variable2)) {
echo '$variable2 is a boolean.';
} else {
echo '$variable2 is not a boolean.';
}
// Check if $variable3 is a boolean
if (is_bool($variable3)) {
echo '$variable3 is a boolean.';
} else {
echo '$variable3 is not a boolean.';
}In this example, $variable1 is a boolean, so the first condition is true, and it prints that $variable1 is a boolean. However, for $variable2 and $variable3, which are not booleans, so, the conditions are false, and it prints that they are not booleans.
This function is useful when you need to validate or check the type of a variable in your PHP code, especially when dealing with user input or external data.
Anyway, let’s explore how to use the Boolean data type in control structures.
PHP Boolean Values in Control Structures
PHP is a flexible language. When you need to check if a variable has a value, but you are not certain about its existence, you can use an if statement to determine whether the variable has a value or not. This process automatically yields a boolean value.
Here is an example:
<?php
$value = "CodedTag";
// true statement ( It has a value )
if( $value ) {
echo "This condition yields a truth value.";
}
?>Or you can use the boolean value directly.
<?php
$value = "CodedTag";
// true statement ( It has a value )
if( $value == true ) {
echo "This condition yields a truth value.";
}
?>Anyway, let’s summarize.
Wrapping Up
In this tutorial, you have understood what the PHP boolean data type is. Here are a few points to summarize this guide:
- A PHP boolean is a logical data type that has two values: true and false.
- True and false are case-insensitive, which allows you to write uppercase or lowercase.
Thank you for reading. Happy Coding!
Similar Reads
The PHP array_intersect function finds common values in arrays and returns matches as a new array. Understand the array_intersect in…
The array_intersect_key in PHP compares arrays by keys and returns only the parts that match. It is useful when you…
Variables in PHP are general elements used for data storage and other manipulations. Global variables belong to a particular category…
So, what is a function? Quite simply, a function in PHP is a set of instructions you write once, and…
The PHP OR operator has two renditions: either || or or. Both of these are useful logical operators when you want to introduce…
The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…
If you want to write good PHP code, strict mode should be on your radar. Strict mode—activated by the command declare(strict_types=1); at…
Today, we will discuss the most prominent and useful superglobal in PHP, $_REQUEST, to get user input. The $_REQUEST array lets you retrieve…
When you first step into web development, one name seems to pop up everywhere: PHP. The PHP programming language has…
The PHP range function helps you make sequences. Without it, you would write loops every time you need numbers or…