PHP  Operators

an operator is a symbol that represents a specific action, a plus sign(+) is an operator that represents addition.

the values and variables that are used with an operator are known as operands.


PHP arithmetic operators

PHP arithmetic operators are used to perform arithmetic operation between variables and/or values.

the table below explains the arithmetic operators: where y=6:

operatordescriptionexampleresult of xtry it
+additionx = 5+y;11 
-subtractionx = y-42 
*multiplicationx = 4*y24 
/divisionx = y/23 
%modulus (remainder)x = y%20 
   <?php
   
               $x = 20 ;
               
               $y = 3 ;
               
                        echo   "addition = ". ($x + $y);                      // output  23
                        
                        echo   "<br/>subtraction =". ($x - $y);        // output is 17
                        
                        echo   "<br/>multiplication =". ($x * $y);   // output is 60
                        
                        echo   "<br/>division = ". ($x / $y);        // output is 6.6666666
                        
                        echo   "<br/>modulus =". ($x % $y);    // output is 2                    
    ?>

PHP assignment operators(shorthand operators)

the assignment operator in php is '='. it can be used to assign a value to a variable. other assignment operators are shorthand for the operations.

an assignment operator assigns a value to its left operand based on the value of its right operand.

x=8 and y=2, the table below explains the assignment operators:

operatorexpanded meaningexampleoutputtry    it
= = a = ba=2 
+= a = a + b a += b a = 10 
-= a = a - b a -= b a = 6  
*= a = a * b a *= b a = 16 
/= a = a / b a /= b a = 4 
%= a = a % b a %= b a = 0 
&= a = a & ba &=ba = 0 
|= a = a | b a |= ba = 10 
^= a = a ^ b a ^= ba = 10 
<<= a = a << aa <<=ba = 32 
>>= a = a >> ba >>= ba = 2 
   <?php
   
                   $x = 20 ;
                   
                   $y = 10 ;
                   
                              echo "x is = $x  y is = $y ";
                              
                              $x +=120 ;
                              
                              echo " x  after operation = $x ";
                              
                              $x -=$y;
                              
                              echo " x after subtraction = $x ";
                              
                              $y *= $y ;
                              
                              echo " y after multiplication = $y ";
   ?>

PHP increment and Decrement Operator

the increment ( ++ ) / decrement ( - - ) operators only affect numbers and strings. arrays, objects and resources are not affected. decrementing null values has no effect too, but incrementing them result in 1.

the table below explains the increment and decrement operators:

operatornamedescriptiontry_it
++$ipre incrementfirst increments $i by one, then return $i. 
$i++post incrementreturns $i, then increments $i by one. 
--$ipre decrementfirst decrement $i by one, then return $i. 
$i--post decrementreturn $i, then decrements $i by one. 

note: when using the ++ operator by itself on a variable, ++$var is faster than $v++ and uses slightly less memory .


PHP String operator

the string concatenation operator ( . ), which returns the concatenation of its right and left string arguments.

operatordescription
.string concatenation
.=string concatenation assignment (shorthand notation)
    <?php
    
                   $str =  " san" . "deep" . " kum" ."ar";
                   
                   $st  = "soft";
                   
                   $st .= "ware";
                   
                   $st .= " development";
                   
                       
                         echo   ($str) ;
                         
                         echo   "<br/>"; echo ($st);
                         
                   $st = $st . $str;
                   
                         echo   "<br/>{$st}"; 
    ?>

PHP Array operators

the php provide some operators that are used to compare arrays

operatornamedescription
+unionunion of $a and $b
==equalitytrue if $a and $b have the same key/value pairs
===identitytrue if $a and $b have the same key/value pairs in the same order and of the same types.
!=Inequalitytrue if $a is not equal to $b
<>Inequalitytrue if $a is not equal to $b.
!==Non-identitytrue if $a is not identical to $b.
    <?php
                                    $a = array("x"  => "apple", "y" => "yellow");
                                    
                                    $b = array("z"  => "orange", "u" => "yellow");
                                    
                                    $c = $a + $b ;  // union of $a and $b
                                    
                                       var_dump($c);
                                       
                                       var_dump($a == $b);
                                       
                                       var_dump($a === $b);
                                       
                                       var_dump($a != $b);
                                       
                                       var_dump($a <> $b);
                                       
                                       var_dump($a !== $b);
  ?>