PHP Logical Operator and Usage Part-2



In part 1 we have discussed about logic and var_dump() function. In this part we will discuss about usage of var_dump() function, if, else, else if and examples. We have used $personOne and $personTwo in last part 1 where we used 35 for $personOne and 40 for $personTwo. Let's compare and find out the boolean output from var_dump(). 


<!--?php

    $personOne = 35;
    $personTwo = 40;
    var_dump($personOne --> $personTwo); //condition is false

?>

Output:


bool(false)

  We used greater(>) sign, so var_dump() will give false. Because $personOne is not bigger than $personTwo. But if we use less(<) than sign then var_dump() give true.


<!--?php

    $personOne = 35;
    $personTwo = 40;
    var_dump($personOne < $personTwo); //condition is true

?-->

bool(true)

Now condition is true. For this reason var_dump() returns true. You can check any kind of variable type or condition using this simple  var_dump() function. Let's see some real life example of if, else and elseif

Example 1 For IF Condition


<!--?php

    $personOne = 35;
    $personTwo = 40;
    if ($personOne < $personTwo) {
        echo "Person is smaller";
    }
    
?-->

Output:


Person is smaller

 

Example 2 For IF Condition


<!--?php

    $firstNum = 10;
    $secondNum = 20;
    if ($firstNum!=$secondNum) {
       echo "Two numbers are not same";
    }
    
?-->

Output:


Two numbers are not same

   

Example of if else Condition


<!--?php

    $firstNum = 20;
    $secondNum = 20;
    if ($firstNum==$secondNum) {
       echo "Two numbers same";
    }else{
         echo "Two numbers are not same";
    }
    
?-->

Two numbers same

 

Example of  else if Condition


<!--?php

    $marks = 45;

    if ($marks -->= 80) {
        echo "You have got GPA 4.00";
    } else if ($marks >= 75) {
        echo "You have got GPA 3.75";
    } elseif ($marks >= 70) {
        echo "You have got GPA 3.50";
    } elseif ($marks >= 65) {
        echo "You have got GPA 3.25";
    } elseif ($marks >= 60) {
        echo "You have got GPA 3";
    } else if ($marks >= 55) {
        echo "You have got GPA 2.75";
    } else if ($marks >= 50) {
        echo "You have got GPA 2.50";
    } else if ($marks >= 45) {
        echo "You have got GPA 2.25";
    } else if ($marks >= 40) {
        echo "You have go GPA 2.00";
    } else {
        echo "You have failed";
    }
    
?>

Output


You have got GPA 3

Hope that you have got a better ideas about conditional logic. This page will be modified also in future with several examples and explanation.