This is one of the most important topic which we are going to learn today so do not distract while learning the totorial, you may put your phone on silent mode so that gf may not disturb you right now (just fun). Let’s start : as we know that ALPHABETS are the basics to write a paragraph, in the same manner Operators are used in php. Senior experienced developers also commits mistakes while using this. Even in interview the tricky/confusing questions are asked using operators. So i am giving you a simple hint to pay extra attention on this topic and if there is any doubt, you may let us know by dropping your comments.

## What are operators ?

Operators are the thing which is used to perform some operation on data, based on the type of operator we are using. In php we use several types of operator as mentioned below.

### Types of operator in php

- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators

Now we will study in details about each type of data type. so first we will start with Arithmetic operators.

## Arithmetic Operators

PHP arithmetic operators are those operators which are used to perform addition, subtraction, multiplication, division, modulus e.t.c

Arithmetic Operator | Name | Description |

+ | Addition | ($p + $q) , Sum of $p and $q |

– | Substraction | ($p – $q) , Substraction of $p and $q |

* | Multiplication | ($p * $q) , Multiplication of $p and $q |

/ | Division | ($p / $q) , Division of $p and $q |

% | Modulus | ($p % $q) , Remainder of $p divided by $q |

** | Exponentiation | ($p ** $q) , $q is exponent of $p (from php 5.6) |

Lets see an example of php arithmetic operator.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php $p = 15; $q = 2; echo 'Addition of '.$p.' and '.$q.' is ==> '.($p + $q); echo "<br>"; echo 'Difference of '.$p.' and '.$q.' is ==> '.($p - $q); echo "<br>"; echo 'Product of '.$p.' and '.$q.' is ==> '.($p * $q); echo "<br>"; echo 'Division of '.$p.' and '.$q.' is ==> '.($p / $q); echo "<br>"; echo 'Modulus of of '.$p.' and '.$q.' is ==> '.($p % $q); echo "<br>"; //echo 'Exponentiation of $p and $q is '.$p ** $q; // if your php version in 5.6 remove comment of the above line and check. /* Output : Addition of 15 and 2 is ==> 17 Difference of 15 and 2 is ==> 13 Product of 15 and 2 is ==> 30 Division of 15 and 2 is ==> 7.5 Modulus of of 15 and 2 is ==> 1 */ ?> |

## PHP Assignment operators

PHP assignment operators are used to assign/write the value to the variable. In PHP the assignment operator is denoted by ” = ” . Which says that the value or expression on the right side of = is assigned to the left operand i.e variable.

Assignment Operator | is same as | Description |

$p = $q | $p = $q | Value of $q is assigned to $p |

$p += $q | $p = $p + $q | Addition of $p, $q assigned to $p |

$p -= $q | $p = $p – $q | Difference of $p and $q assigned to $p |

$p *= $q | $p = $p * $q | Product of $p and $q assigned to $p |

$p /= $q | $p = $p / $q | Division of $p by $q assigned to $p |

$p % $q | $p = $p % $q | Modulus of $p , $q assigned to $p |

let’s take an example of php assignment operator.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php $p = 5; $q = 2; $r = 3; $s = 5; $t = 8; $u = 2; $v = 10; $w = 2; $x = 16; $y = 4; $c = 22; $d = 5; // one $p = $q ; echo $p; // output 2 echo "<br><br>"; // two $r += $s; echo $r; // output 8 echo "<br><br>"; // three $t -= $u; echo $t; // output 6 echo "<br><br>"; // four $v *= $w; echo $v; // output 20 echo "<br><br>"; //five $x /= $y; echo $x; // output 4 echo "<br><br>"; //six $c %= $d; echo $c; // output 2 echo "<br><br>"; ?> |

## PHP Comparison operators

PHP Comparison operators are those operators which is used to compare two values. These are basic operators which you might know but go through once to clear the concepts. There are several types of PHP comparison operators which i am listing down.

Comparison Operator | Name | Description |

== | Equal | ($p == $q), returns true if $p is equal to $q. |

=== | Identical | ($p === $q), returns true if $p is equal to $q, and $p and $q are of same type. |

!= | Not equal | ($p != $q), returns true if $p is not equal to $q. |

<> | Not equal | ($p <> $q), returns true if $p is not equal to $q. |

!== | Not identical | ($p !== $q), returns true if $p is not equal to $q or $p and $p are not of same type |

> | Greater than | ($p > $q), returns true if $p is greater than $q. |

< | Less than | ($p < $q), returns true if $p is less than $q. |

>= | Greater than or equal to | ($p >= $q), return true if $p is greater than or equal to $q |

<= | Less than or equal to | ($p <= $q), return true if $p is less than or equal to $q |

let’s see an example of php comparison operator below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php $p = 2; $q = 2; echo "Example 1 (==) : => "; var_dump($p == $q); // Ouput : bool(true) echo "<br><br>"; echo "Example 2 (===) : => "; $x = 10; $y = "10"; var_dump($x === $y); // Ouput : bool(false) since, $x is integer and $y is string. echo "<br><br>"; echo "Example 3 (!=) : => "; $y = 12; $z = 12; var_dump($y != $z); // Ouput : bool(false) echo "<br><br>"; echo "Example 4 (!==) : => "; $y = 12; $z = "12"; var_dump($y !== $z); // Ouput : bool(true) echo "<br><br>"; echo "Example 5 (>) : => "; $y = 14; $z = 12; var_dump($y > $z); // Ouput : bool(true) echo "<br><br>"; echo "Example 6 (<) : => "; $y = 13; $z = 12; var_dump($y < $z); // Ouput : bool(false) echo "<br><br>"; echo "Example 7 (>=) : => "; $y = 14; $z = 2; var_dump($y >= $z); // Ouput : bool(true) echo "<br><br>"; echo "Example 8 (<=) : => "; $y = 14; $z = 7; var_dump($y <= $z); // Ouput : bool(false) echo "<br><br>"; ?> |

## PHP Increment and Decrement Operators

As we know that, Increment means to increase and decrement means decrease. While dealing with PHP operators its meaning is same here also. Increment and decrement operators are used to increase or decrease the value of the variable. Lets see how it happens.

Increment/Decrement Operator | Name | Description |

$i++ | Post Increment | Return $i then increment $i by one. |

++$i | Pre Increment | Increment $i by one and then return $i |

$i– | Post Decrement | Return $i and then decrement $i by one. |

–$i | Post Decrement | Decrement $i by one and then return $i |

You have seen we used two names (i.e post and pre increment/decrement). A question arises now arise is : What is post and pre increment/decrement in php ?.

Lets understand this through by examples.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php $x = 10; echo $x++; echo "<br>"; // Output is still 10 because // "This is POST INCREMENT : It first return x then it add one, // so output is still 10" $x = 10; echo ++$x; echo "<br>"; // Output is 11 because //"This is PRE INCREMENT : It first add one then return value of x, //so output is 11"; echo "<h3> similarly decrement works </h3>"; $x = 20; echo $x--; echo "<br>"; // Output is still 20 because // "This is POST DECREMENT : It first return x then it substract one, // so output is still 20"; $x = 20; echo --$x; // "This is PRE DECREMENT : It first substract one then return value of x , // so output is 19"; ?> |

## PHP Logical Operators

Logical operators are generally used in conditional statement. Whenever you require some output based on some condition then we use logical operators.

Lets see how it works