PHP Comparison operators

the PHP comparison operators are used to compare two values.it can be used in logical statements.

the result of a comparison operation is always true or false.

the table below explains the comparison operators:

operatornameexampleresult
==equal$a == $btrue if $a is equal to $b
===identical$a === $btrue if $a is equal to $b, and they are of the same type
!=not equal$a != $btrue if $a is not equal to $b
<>not equal$a <> $btrue if $a is not equal to $b
!==not identical$a !== $btrue if $a is not equal $b, or they are not of the same type
<less than$a < $btrue if $a is strictly less than $b
>greater than$a > $btrue if $a is strictly greater than $b
<=less than or equal to$a <= $btrue if $a is less than or equal to $b
>=greater than or equal to$a >= $btrue if $a greater than or equal to $b
     <?php
       
           $x = 122;
           
           $y = 132;
           
           
                var_dump( $x == $y);   echo "<br/>";     //output false
                
                var_dump( $x >= $y);   echo "<br/>";   //output false
                
                var_dump( $x <= $y);   echo "<br/>"; //output true
           
                var_dump( $x !== $y);   echo "<br/>"; //output true
  ?>    

PHP logical operators

php logical operators are typically used with boolean values,when they are, they return a boolean value.

the logical operators can only be applied to boolean operands.

the result of a logical operation is always true or false.

operatornameexampleresult
andand$a and $btrue if both $a and $b are true
oror$a or $btrue if either $a or $b is true
xorxor$a xor $btrue if either $a or $b is true, but not both
!not!$atrue if $a is not true
&&and$a && $btrue if both $a and $b are true
||or$a || $btrue if either $a or $b is true

note : && has a greater precedence than 'and'.

note : || has a greater precedence than 'or'.

    <?php
    
            $x = 5 ;  $y = 6;  $bool = true ;
                             
                      var_dump($x <= $y && 12 !=13); 
                             
                      var_dump($x >= $y && 12 !=13); 
                             
                      var_dump(20 <=22  || 20 == 21);
                             
                      var_dump(21 == 21 xor 22 == 23 );
                             
                      var_dump(21 == 21 xor 22 == 22); 
                             
                      var_dump(!$bool); 
                             
                      var_dump(!false); 
      ?>

PHP Bitwise Operators

operatornameexampleresult
&and$x & $yBits that are set in both $x and $y are set.
|or$x | $yBits that are set in either $x or $y are set.
^xor$x ^ $ybits that are set in $x or $y but not both are set
~not~$xBits that are set in $x are not set, and vice versa
<<left shift$x << $yshift the bits of $x $y steps to the left(step means 'multiply by two')
>>right shift$x >> $yshift the bits of $x $y steps to the right(step means 'divide by two')

PHP Ternary Operator

the php ternary conditional operator returns one of two values based on the logical value of the condition.

the conditional operator  ? :  also called the 'ternary' operator.

syntax:

         $output = (condition) ? statement__1 : statement__2 ;

if condition is true, the statement__1 is execute otherwise the statement__2 is executed.

<?php
  
            $x = 20 ;
                     
            $y = 30 ;
                     
                         $z  = ( $x <= $y ) ? "this is true part" : "this is false part" ;
                         
                         $zz = ( $x >= $y ) ? "this is true part" : "this is false part" ;
                               
                         var_dump($z);
                         
                         var_dump($zz);
   ?>

PHP instanceof Operator

the php instanceof operator is used to determine whether a php variable is an instantiated object of a certain class.

to check if an object is not an instanceof a class, the logical not ( ! ) operator can be used.

the php instanceof operator can also be used to determine whether a variable is an instantiated object of a class that implements an interface.

 <?php
   
      interface   cars{   }
             
      class     audi  implements  cars{    }
             
      class     tata   extends   audi {    }
             
      $tata_obj  =  new  tata();
             
            if( $tata_obj  instanceof  tata ) echo "instance of tata class" ;
             
            if( $tata_obj  instanceof  audi ) echo "<br/>instance of audi class <br/>";
             
            if( $tata_obj  instanceof  cars ) echo "instance of cars class";

  ?>