Before understanding what is PHP Boolean, let’s understand what is Boolean?
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
Let’s take a look at different types of boolean values:
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.
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.
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.
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.
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.
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.
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.
The above is the detailed content of PHP Booleans. For more information, please follow other related articles on the PHP Chinese website!