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
Understanding how to update data in PHP and MySQL is like editing a draft—it is all about tweaking the right…
The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…
PHP Variadic functions show you a way to handle a variable number of arguments within a function. They are designed…
PHP’s if-elseif statement is a fundamental construct that allows developers to control the flow of their code based on different conditions. In…
Developers needed a way to check the length of a string, so the strlen function was introduced to handle this…
PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…
Understanding PHP syntax is like the basics of any language—PHP syntax defines the rules for writing code that a server…
Sometimes things go wrong when PHP runs the code. So, you need to know the reasons for this error to…
Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result…
PHP added the array_map() function to help you apply one function to each item in an array. Understand the array_map…