You are reading the article Comprehensive Guide To Php Booleans updated in September 2023 on the website Happystarlongbien.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Comprehensive Guide To Php Booleans
Introduction to PHP BooleansBefore understanding what is PHP Boolean, let’s understand what is Boolean?
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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 ValueLet’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 #1Let’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 #2Let’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 #3Let’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 #4The 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;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 #5Similar 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;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.
ConclusionIn 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 ArticlesThis 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 –
You're reading Comprehensive Guide To Php Booleans
Update the detailed information about Comprehensive Guide To Php Booleans on the Happystarlongbien.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!