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.
Table of Content
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
PHP variable function is a variable that is used to assign a function name with parentheses (). However, the PHP interpreter…
A loop in PHP helps you run tasks without repeating the same lines. In this article, you will learn how…
PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…
User input can be unpredictable and dangerous. A simple form submission might carry malicious code or invalid data and lead…
PHP introduced null to handle undefined or missing values. It helps prevent errors when you check if a variable exists.…
You always start with “Hello World” when you learn a new language. It keeps things simple. You see right away…
PHP array_intersect_uassoc checks two or more arrays and returns matches. It compares both the values and the keys with a…
The PHP array_intersect function finds common values in arrays and returns matches as a new array. Understand the array_intersect in…
Today, we will discuss the most prominent and useful superglobal in PHP, $_REQUEST, to get user input. The $_REQUEST array lets you retrieve…
The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…