Updated March 17, 2023
Introduction to PHP Booleans
Before understanding what is PHP Boolean, let’s understand what is Boolean?
Boolean is a data type that is used in most computer programming languages like Java, Python, C, PHP, etc. It is a data type that has one or two possible values (either true or false). It is intended to represent the two truth values of logic and Boolean algebra. Depending upon the conditions it will set its value as 1(true) or 0(false). This data type is used by many programming languages to check if the condition satisfies and the statements get executed.
PHP Boolean
In PHP, the boolean data type is used to set the values of the variables. It is mostly used for conditional statements like If, While, For, Switch, Etc. These conditional and iterative commands are mostly defined to test these boolean-valued expressions. Boolean value returns only two values i.e. either true or false. so, it is used in conditional statements to pass through that particular condition to execute the following statements corresponding to it.
Types of PHP Booleans Value
Let’s take a look at different types of boolean values:
- Integer: This Boolean value is used to check the condition of whether the variable’s output is non-zero. If the output is zero, then the condition is false and the statements will not be executed presently inside the loop and will skip the loop and execute the further statements.
- Floating Point: This Boolean value is used to check the condition of whether the variable’s output is a floating number for e.g. 0.0. If the output is non-zero, then the condition is true and the loop statements will be executed, if the output is zero then the statements inside the loop will be skipped and will proceed to execute the further statements.
- Strings: This Boolean value is used to check whether the string is empty or not. If the output of the conditional statement is true, then the output will be a string value and the statements inside the loop will get executed. If the output is false, then the output is either a zero string or an empty string.
- Array: This Boolean value is used to check whether an array has elements in it. If the condition is true, then it must be having at least one number of element and the statements inside the loop will get executed. If the condition is false, then the output must be an empty array and will skip the statements inside the loop and will proceed to execute the further statements.
- NULL: This Boolean value is used to check whether the variable’s value is NULL. A variable’s value will be NULL if it is initialized to NULL at the beginning itself or it has not been set any value or it is unset. If the condition is true, then statements inside the loop will get executed. If the output is false, it will skip the statements inside the loop and will proceed to execute the further statements.
- Objects: This Boolean value is used to check whether an object is present or not. If it is present, then the condition is true and the statements will be executed and the condition is false, then it will skip the statements inside the loop and will proceed to execute the further statements.
How Boolean Value Works?
Boolean values are nothing but 0 and 1 i.e. either true or false. if the condition satisfies, it is true else it is false.
Example #1
Let’s consider simple examples to understand how Boolean value works.
Code:
<?php
$selling_price = 75.5;
$cost_price =50;
if ($selling_price == 0)
{
echo "The selling price should be a non zero";
}
else
{
echo "The selling price is $selling_price";
}
?>
Output:
In the above example, the output is a non-zero. Therefore, the statements inside if the statement is not executed.
Example #2
Let’s take another example for string Boolean value:
Code:
<?php
$a="Leela";
$b="Swamy";
if ($a)
{
echo "The name of the person is ".$a.$b;
}
else
{
echo "The string is empty";
}
?>
Output:
In the above example, the name is non-empty and also no comparison operator is used. PHP automatically converts the value and sets it to its Boolean equivalent true. So the statements will be executed written inside if statement.
Example #3
Let’s take another example:
Code:
<?php
$var=NULL;
$var1=500;
if ($var == NULL)
{
$var=$var1;
echo "The value of var is $var";
}
?>
Output:
In the above example, the $var variable has been initialized to null. So the condition becomes true and the statement written inside the loop gets executed and sets the value to 500.
Example #4
The function is_bool () can be used to check whether a variable contains a Boolean value or not. The is_bool () is an inbuilt function in PHP. It is a Boolean function so it returns TRUE when the value is a Boolean value, otherwise FALSE. Let’s take a simple example.
Code:
<?php
$a=TRUE;
$b=FALSE;
echo $a.'<br>';
echo $b.'<br>';
echo is_bool($a).'<br>';
echo is_bool ($b).'<br>';
?>
Output:
In the above example, the output of the function is_bool() will be either 0 or 1. In this example, the output will be 1 and after the break also it will be 1. It just checks whether the Boolean value is set to the variable and in this example, both the variables have been initialized Boolean values.
Example #5
Similar to is_bool () function, we have a function called var_dump () function to print both the type as well as the value as output. This function is used to print the data type associated with the variable that the developers want to print on the screen.
Code:
<?php
$a = true;
echo $a.'<br>';
echo is_bool($a).'<br>';
var_dump($a).'<br>';
?>
Output:
In the above example, the output of is_bool() function and var_dump function() differs. The var_dump() function will print the data type along with the value of the variable.
Conclusion
In this article, we discussed the Boolean value and its types. Many of the Boolean types are used in many programs and applications. Boolean values play a vital role in many programming languages especially in PHP where we use cookies and sessions to check whether they are set or unset.
Recommended Articles
This is a guide to PHP Booleans. Here we discuss the basic concept with different types of PHP booleans, it’s working along with examples and code implementation. You may also look at the following articles to learn more –