PHP Operators

PHP Arithmetic Operators :

+ - Addition      $a + $b
- - Subtraction      $a - $b
* - Multiplication    $a * $b
/ - Division              $a / $b
% - Modulus      $a % $b

PHP Assignment Operators :



x = y        x = y       The left operand gets set to the value of the expression on the right



x += y     x = x + y   Addition
x -= y      x = x - y    Subtraction
x *= y      x = x * y   Multiplication
x /= y      x = x / y    Division
x %= y    x = x % y   Modulus


Example :



<?php 

       $x=10; 
       $x += 100;
       echo $x;      // outputs 110
?>


PHP String Operators :

. Concatenation                $txt1 = "Hello"
        $txt2 = $txt1 . " world!"
                               result :       $txt2 contains "Hello world!"

.= Concatenation assignment $txt1 = "Hello"
        $txt1 .= " world!"
      result :       $txt1 contains "Hello world!"

PHP Increment / Decrement Operators :

++$x         Pre-increment Increments $x by one, then returns $x
$x++         Post-increment Returns $x, then increments $x by one
--$x         Pre-decrement Decrements $x by one, then returns $x
$x--    Post-decrement Returns $x, then decrements $x by one


Example : 
 <?php
     $x=10; 
     echo ++$x;  //outputs 11
 ?>

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Operator             Name                                    Example              Result


==                       Equal                                     $x == $y               True if $x is equal to $y
===                     Identical                               $x === $y            True if $x is equal to $y, and they are of the same type
!=                       Not equal                               $x != $y               True if $x is not equal to $y
<>                      Not equal                              $x <> $y               True if $x is not equal to $y
!==                     Not identical                        $x !== $y              True if $x is not equal to $y, or they are not of the same type
>                        Greater than                          $x > $y                  True if $x is greater than $y
<                       Less than                                $x < $y                  True if $x is less than $y
>=           Greater than or equal to                 $x >= $y               True if $x is greater than or equal to $y
<=           Less than or equal to                     $x <= $y               True if $x is less than or equal to $y


PHP Logical Operators 


Operator Name Example Result

&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true

Comments