Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Comparison and logical operator in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 105
    Comment on it

    Comparison operator is basically used to compare any two variables. Whereas the logical operator is basically used for logical operations such as AND, OR, NOT. We know that there is precedence for the different operators we have a particular table for the precedence of the operators. The output that is evaluated is based upon the precedence table that is predefined.

    Comparison operator:

      

    Example Name Result
    $a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (PHP 4 only)
    $a <> $b Not equal TRUE if $a is not equal to $b.
    $a < $b Less than TRUE if $a is strictly less than $b.
    $a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
    $a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.
    $a > $b Greater than TRUE if $a is strictly greater than $b.
    $a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (PHP 4 only)
    $a != $b Not equal TRUE if $a is not equal to $b.
    $a == $b Equal TRUE if $a is equal to $b.

    Logical Operator 

    Example Name Result
    $a and $b And TRUE if both $a and $b are TRUE.
    $a or $b Or TRUE if either $a or $b is TRUE.
    ! $a Not TRUE if $a is not TRUE.
    $a xor $b Xor TRUE if either $a or $b is TRUE, but not both.
    $a || $b Or TRUE if both $a and $b are TRUE.
    $a && $b And TRUE if both $a and $b are TRUE.

    Example of logical operator

    <?php
    
    // --------------------
    // foo() will never get called as those operators are short-circuit
    
    $p = (false && foo());
    $q = (true  || foo());
    $r = (false and foo());
    $s = (true  or  foo());
    
    // --------------------
    // "||" has a greater precedence than "or"
    
    // The result of the expression (false || true) is assigned to $e
    // Acts like: ($e = (false || true))
    $a = false || true;
    
    // The constant false is assigned to $f before the "or" operation occurs
    // Acts like: (($f = false) or true)
    $b = false or true;
    
    var_dump($a, $b);
    
    // --------------------
    // "&&" has a greater precedence than "and"
    
    // The result of the expression (true && false) is assigned to $g
    // Acts like: ($g = (true && false))
    $c = true && false;
    
    // The constant true is assigned to $h before the "and" operation occurs
    // Acts like: (($h = true) and false)
    $d = true and false;
    
    var_dump($c, $d);
    ?>

    Output of above code is :

    bool(true)
    bool(false)
    bool(false)
    bool(true)

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: