PHP Logical Operator and Usage Part-1



PHP logical operators are mainly used for checking several types of condition such as true, false, greater, smaller, equal, not equal etc. This post is divided into two parts. Those are part 1 and part 2. In part 1 we will discuss about introduction of logical operators. In part 2 we will discuss about example and better explanation of conditional logic .There are several types of operators for doing this work. You can look down the table given below for getting ideas.  

Name Operator Usage
Equal = To Assign Value
Equal == To Check Equality
Not Equal != To Check Not Equal
Identical Operator === To Check Equal By Value
Non Identity !== To Check Not Equal By Value
Greater > Greater
Smaller < Smaller
Greater or Equal >= To Check Greater or Equal
And && For Checking Logic
OR || For Checking Logic

Usage of if Condition

Mainly for checking a condition if is used. This if checks a condition. After becoming the condition true, then next process is done. if takes condition inside its parenthesis and after that it is needed to add curly braces {} for writing body code inside it. Usually, for single line of code after if condition, no need to add curly braces. But for more than one line of code after if condition it is mandatory to add curly braces{}. Good practice is to use curly braces every time. For declaring if condition you need to follow below code.


<!--?php

    if (condition) {
        //code  line 1
        //code line 2
        //code line 3
    }
    
    if(condition)
        //code line 1
?-->

 

Use of else Condition

If if condition returns false any time then else condition will be activated. That means else is dependent to if. But if condition is totally independent. For using else, if is mandatory for all the time. else has not any type of condition. It works after if condition becomes false.  But for using if condition else is optional. Code will give you clear idea.


<!--?php

    if (condition) {
        //code  line 1
        //code line 2
        //code line 3
    }else{
        //code  line 1
        //code line 2
        //code line 3
    }
    
    if(condition)
        //code line 1
        else
            //code line 1
?-->

 

Usage of elseif() Condition

Like other programming language php also allows you to check multiple conditions by using else if. You can use as many as condition you need to check by using else if. There is an example structure for using else if.


<!--?php

    if (condition) {
        //code  line 1
        //code line 2
        //code line 3
    }elseif(condition){
        //code  line 1
        //code line 2
        
    }else if(condition){
        //code  line 1
        //code line 2
    }else{
        //code  line 1
        //code line 2
    }
    
?-->

  There is a built in function in php named var_dump(). This function gives the type of variable. This function is usually used for finding the type of a variable as it return true or false. We can see some example of using logical operators in real life. Let's see some example. Two variables $personOne, $personTwo. In this variable we will assign age of two persons differently. First personOne age is 35 and personTwo age is 40. Now We will check who is older by age. Details in Next part....