# PHP Operators

PHP operator is like a function, which is used to perform operations on variables and values.

**PHP operators are categorized into the following groups :**

- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Increment/Decrement Operators
- Logical Operators
- String Operators
- Array Operators
- Error Control Operators
- Execution Operators

##

**PHP Arithmetic Operators**

Arithmetic operators are used to performing various operations such as addition, subtraction, multiplication, etc, These operators are used with numeric values.

Operator | Name | Operation | Result |
---|---|---|---|

+ | Addition | $m + $n | Sum of $m and $n |

- | Subtraction | $m - $n | Difference of $m - $n |

* | Multiplication | $m * $n | Product of $m * $n |

/ | Division | $m / $n | Quotient of $m and $n |

% | Modulus | $m % $n | Remainder of $m divided by $n |

** | Exponentiation | $m ** $n | Result of raising $m to the $n'th power |

**PHP Assignment Operators**

In PHP, the Assignment operator is used to writing value to a variable.

- If a value is numeric you can both write to variable and can do an operation.
- If a value is a string you can only write to variable.

Assignment | Assigned to | Description |
---|---|---|

$m = $n | $m = $n | The left operand gets set to the value of the expression on the right |

$m += $n | $m + $n | Addition |

$m -= $n | $m - $n | Subtraction |

$m *= $n | $m * $n | Multiplication |

$m /= $n | $m / $n | Division |

$m %= $n | $m % $n | Modulus |

**PHP Comparison Operators**

Comparison operators are used to compare two type data numeric and string.

Operator | Name | Operation | Result |
---|---|---|---|

== | Equal | $m == $n | True, If $m is equal to $n. |

=== | Identical | $m === $n | TRUE if $m is equal to $n, and they are of the same type. |

!= | Not Equal | $m != $n | TRUE if $m is not equal to $n. |

<> | Not Equal | $m <> $n | TRUE if $m is not equal to $n. |

!== | Not Identical | $m !== $n | TRUE if $m is not equal to $n, or they are not of the same type. |

< | Less Than | $m < $n | TRUE if $m is strictly less than $n. |

> | Greator than | $m > $n | TRUE if $m is strictly greater than $n. |

<= | Less than or Equal To | $m <= $n | TRUE if $m is less than or equal to $n. |

>= | Greator than or Equal To | $m >= $n | TRUE if $m is less than or equal to $n. |

<=> | Spaceship | $m <=> $n | An integer less than, equal to, or greater than zero when $m is respectively less than, equal to, or greater than $n, Available in PHP 7.0 and 7+. |

**PHP Increment/Decrement Operators**

PHP supports C programming style PRE & POST increment and decrement of the variable.

- The PHP increment operator is used to increase the variable's value.
- PHP decrement operator is used to decrease the variable's value.

Operator | Name | Description |
---|---|---|

++$m | Pre Increment | Increments $m by one, then returns $m. |

$m++ | Post Increment | Returns $m, then increments $m by one. |

--$m | Pre Decrement | Decrements $m by one, then returns $m. |

$m-- | Post Decrement | Returns $m, then decrements $m by one. |

**PHP Bitwise Operators**

PHP Bitwise operators allow evaluation and manipulation of specific bits within an integer additionally.

Operator | Name | Operation | Result |
---|---|---|---|

& | AND | $m & $n | BIts are set in both $m and $n |

| | OR | $m | $n | BIts are set in either $m or $n |

^ | XOR | $m ^ $n | BIts are set in either $m or $n but not both are set |

~ | NOT | ~ $m | BIts are not set in $m |

<< | Shift Left | $m << $n | Shift the bits of $m $n steps to the left (each step means "multiply by two") |

>> | Shift Right | $m >> $n | Shift the bits of $m $n steps to the right (each step means "divide by two") |

**PHP Logical Operators**

In PHP logical operators are used to combine conditional statements.

Operator | Name | Operation | Result |
---|---|---|---|

and | AND | $m and $n | TRUE if both $m and $n are TRUE. |

or | OR | $m or $n | TRUE if either $m or $n is TRUE. |

xor | XOR | $m xor $n | TRUE if either $m or $n is TRUE, but not both. |

&& | AND | $m && $n | TRUE if both $m and $n are TRUE. |

|| | OR | $m || $n | TRUE if either $m or $n is TRUE. |

! | NOT | !$m | TRUE if $m is not TRUE. |

**PHP String Operators**

PHP has two string operators to concatenation and another append the first variable to the second variable.

Operator | Name | Operation | Result |
---|---|---|---|

. | Concatenation | $var1.$var2 | Concatenation of $var1 and $var2 |

.= | Concatenation assignment | $var1.=$var2 | Appends $var2 to $var1 |

**PHP Array Operators**

In PHP, The PHP array operators are used to compare two array variables.

Operator | Name | Operation | Result |
---|---|---|---|

+ | Union | $m+$n | Union of $m and $n |

== | Equality | $m==$n | Returns true if $m and $n have the same key/value pairs |

=== | Identity | $m===$n | Returns true if $m and $n have the same key/value pairs in the same order and of the same types |

!= | Inequality | $m!=$n | Returns true if $m is not equal to $n |

<> | Inequality | $m<>$n | Returns true if $m is not equal to $n |

!== | Non-Identity | $m!==$n | Returns true if $m is not identical to $n |

**PHP Error Control Operator**

PHP supports the error control operator. This error control operator prepared to an expression to generate an error message than expression will be ignored.

```
<?php
/* Intentional file error */
$var = @file ("doesn't exist any file") or
die ("Failed to open file '$php_errormsg'");
// it works for any expression, not just functions or code of block:
$value = @$cache[$key];
// will not issue a notice if the index $key does not exist.
?>
```