Boolean Functions PHP Essential Training

Boolean Functions PHP Essential Training

PHP

Boolean Functions PHP Essential Training

Boolean functions are nothing but the variables which can store either 0 or 1. This means it is used to handle the true or false values on a certain conditions. Boolean Functions PHP Essential Training will gives you an clear idea about check a particular condition is true or false. Let's Jump in to know how it works.
You can notice in our previous Array Functions PHP Essential Training lesson the last function in_array() uses the boolean variable indirectly to show whether the given value is present in the array or not. In such conditions the booleans are very helpful to confirm the status of the variable or functions.
Follow the given example to understand how it works. Before apply the following code make your file with necessary HTML tags. Here we give you only the necessary functions. Download the code for clear details. Here we assign the values called true and false which are not the strings like we assign in our previous examples. They are pre defined keywords for some exact functions.
$bool1 = true;
$bool2 = false;
echo '$bool1: ' . $bool1 . "<br />";
echo '$bool2: ' . $bool2 . "<br />";
Boolean Functions PHP Essential Training
In the previous code you can get a better idea about the true or false. Actually we didn't use the booleans for display something on the screen. We use them for check whether the certain condition is true or not. Let's see how they are helpful to us and check out the functions isset() and unset().
$var1 = 3;
$var2 = "cat";
// Check the $var1 is consist any value or not
echo '$var1 is set: ' . isset($var1) . "<br />";
// Check the $var2 is consist any value or not
echo '$var2 is set: ' . isset($var2) . "<br />";
// Here we didn't initialize a variable $var3 so it returns the false means nothing on display
echo '$var3 is set: ' . isset($var3) . "<br />";
Boolean Functions PHP Essential Training
As we see earlier there is another function called unset() used for erase a data from the variable. After unset the variable $var2 check again the values of the variables.
// Unset the Value of $var2
unset($var2);
// Check the $var1 is consist any value or not
echo '$var1 is set: ' . isset($var1) . "<br />";
// Check the $var2 is consist any value or not
echo '$var2 is set: ' . isset($var2) . "<br />";
// Here we didn't initialize a variable $var3 so it returns the false means nothing on display
echo '$var3 is set: ' . isset($var3) . "<br />";
Boolean Functions PHP Essential Training

Boolean Functions PHP Essential Training

The above examples will helps you to understand how the booleans are working. The next thing in Boolean Functions PHP Essential Training is the NULL. It is used to check the particular variable is NULL or not. Lets see the following example.
// The empty will help you to check the variable is empty or not
echo '$var2 is empty: ' . empty($var2) . "<br />";
Boolean Functions PHP Essential Training
In the above example the var2 is set as empty. The empty() function is checks whether the var2 has a value or not. And it finds the var2 is sets as NULL. So it returns the output as true means 1.
In other case the empty is not only checks the value is available or not. Consider the following example will shows you how the NULL and empty works perfectly.
$var4 = 0;
$var5 = "0";
$var6 = NULL;
$var7 = 7;
echo '$var4 is empty: ' . empty($var4) . "<br />";
echo '$var5 is empty: ' . empty($var5) . "<br />";
echo '$var6 is empty: ' . empty($var6) . "<br />";
// This empty() is finds the variable has a value and returns false
echo '$var7 is empty: ' . empty($var7) . "<br />";
Boolean Functions PHP Essential Training
As conclude the empty is checking the value is NULL or somewhat equivalent to NULL like 0. The empty is also like a boolean but with some little differences. The above example will shows the functionalities of the empty as well.
Hope this Boolean Functions PHP Essential Training will be useful to know some basic functionalities about the isset(), unset(), empty() functions. The next Lesson will be the Type Casting PHP Essential Training
Download the Source Code for the Boolean Function PHP Essential Training from here.

Leave a Reply