Operators indicate compiler which operation to perform. Operators help to perform various calculations on the given data.

Index

## Operators In PHP

PHP provides different types of operators, similar to other programming languages. They are divided into the following categories.

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

### Arithmetic Operators

They are used to perform different arithmetic operations between two or more variable

Operator | Description | Example:(here m = 8, n = 2 ) |
---|---|---|

+ | Addition | $x = $m + $n // Ans: $x=10 |

– | Subtraction | $x = $m – $n // Ans: $x=6 |

* | Multiplication | $x = $m * $n // Ans: $x=16 |

/ | Division | $x = $m / $n // Ans: $x=4 |

% | Modulus | $x = $m % $n // Ans: $x=0 |

### Logical Operators

They are used to perform logical operations like AND Operation, OR Operation, and NOT operations between two or more variables.

Operator | Description | Example:(here m = 8, n = 2 ) |
---|---|---|

&& | AND | $x = ($m > 0) && ($n < 5) // Ans: true |

|| | OR | $x = ($m == 8) || ($n < 0) // Ans: true |

! | NOT | $x = !($n == $m – 6) // Ans: false |

xor | XOR | $x = ($m == 8) xor ($n < 0) // Ans: true |

**NOTE:**

- AND operation returns true if both conditions are true otherwise it is false.
- OR operation is false if both conditions are false; otherwise, it is true.
- NOT operation returns the opposite result of a condition.
- XOR operation is false if both conditions are the same, either true or false otherwise, it is true.
- Logical operations are used to get results for given conditions, and based on that result; further actions are taken.

### Comparison Operators

They are created to compare two or more variables. They are mostly used with logical operators.

Operator | Description | Example:(here m = 8, n = 2 , int) |
---|---|---|

> | greater than | $x = $m > $n // Ans: true |

< | less than | $x = $m < 5 // Ans: false |

>= | greater than Or equal to | $x = $m >= $n // Ans: true |

<= | less than Or equal to | $x = $n <= 2 // Ans: true |

== | equal to | $x = ($m == “8”) // Ans: true |

=== | equal to with Equal type | $x = ($m === 8) // Ans: true |

!= | not equal to | $x = $m != $n //Ans: true |

<> | not equal to | $x = $m <> $n //Ans: true |

!== | not equal to with Not Equal type | $x = $m !== 5 // Ans: true |

<=> | spaceship | $x = ($m <=> $n) |

**Note:** The spaceship is a new operator introduced in PHP 7.

- It returns -1 if $m is less than $n.
- It returns 0 if $m is equal $n.
- It returns +1 if $m is greater than $n.

### Increment – Decrement operator

They are used to increment and decrement the values.

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

++ | Increment |

— | Decrement |

**Note:** Based on the position they are used, we can divide them into two types:

Pre-increment / Post-increment

**Example:** (m =8 , n = 2) Pre increment

$x = $m++; // Ans: x = 8 , m = 9

Post increment

$x = ++$n // Ans: x = 3 , n = 3

Pre decrement / Post decrement

**Example:** (m =8 , n = 2)

Pre decrement

$x = $m–; // Ans: x = 8 , m = 7

Post decrement

$x = –$n // Ans: x = 1 , n = 1

Read Also:What Is GLOBALS in PHP, and How to Use $GLOBALS in PHP

### Conditional Operator

In conditional operators, According to the condition, the result value is assigned to a variable.

They are of two types:

**1) Ternary Operators**

**Syntax:**

Variable name = ( condition ) ? val1 : val2

**Explanation:**

If the condition is true, then val1 is assigned to the variable; otherwise, val2 is assigned to the variable.

**Example:** (m =8 , n = 2)

$x = ($m > 5) ? 7 : 3 // Ans: x = 7

$x = ($n == 5) ? 7 : 3 // Ans: x = 3

**2) Null Coalescing Operators**

**Syntax:**

Variable name = expr1 ?? val2;

**Explanation:**

The value of the variable from expr1 is assigned, If the variable exists in code, otherwise val2 is assigned.

**Example:** (m = 8 , n = 2)

$x = $m ?? 7 // Ans: x = 8

**Explanation:**

Here value of $m variable already exist in the code so that it is assigned to $x.

$x = $p ?? 3 // Ans: x = 3

**Explanation:**

Here value of $p variable doesn’t exist in the code so that 3 is assigned to $x.

### Assignment Operator

It is used to assign value to the PHP variable.

It is denoted by ‘ = ’ sign.

**Example:**

$m = “Errorsea”;

$n = 5;

**Note:** PHP also supports shorthand notations with assignment operators.

Operations | Description | Same as (here m = 8, n = 2 ) |
---|---|---|

$m = $n | assignment | $m = $n // Ans: $m=2 |

$m += $n | Addition | $m = $m + $n // Ans: $m=10 |

$m -= $n | Subtraction | $m = $m – $n // Ans: $m=6 |

$m *= $n | Multiplication | $m = $m * $n // Ans: $m=16 |

$m /= $n | Division | $m = $m / $n // Ans: $m=4 |

$m %= $n | Modulus | $m = $m % $n // Ans: $m=0 |

### Array Operator

PHP supports different operators that are used to compare arrays.

Operator | Name | Example (Here $m = [1,2] , $n = [3,4]) |
---|---|---|

+ | union | $x = ($m + $n) // Ans: [1,2,3,4] |

== | equality | $x = ($m == $) // Ans: false |

=== | identity | $x = ($m === $n) // Ans: false |

!= | inequality | $x = ($m != $n) // Ans: true |

<> | inequality | $x = ($m <> $n) // Ans: true |

!== | Non-identity | $x = ($m !== $n) // Ans: true |

**Explanation:**

- Union operator returns the union of both the arrays.
- Equality operator compares the arrays and returns true if both are equal; otherwise returns false.
- Identity operator compares the arrays and returns true if both are equal and their positions and type are also similar; otherwise, return false.
- Equality operator compares the arrays and returns true if both are equal; otherwise returns false.
- Inequality operator ( != ) compares the arrays and returns true if both are not equal; otherwise returns false.
- Inequality operator ( <> )compares the arrays and returns true if both are not equal; otherwise returns false.
- The non-identity operator compares the arrays and returns true if both are not equal and different; otherwise, returns false.

### String Operator

They are used to concatenate two or more strings in PHP.

**Example 1:**

$e1 = “I am”;

$e2 = “ Errorsea”;

$e3 = $e1.$e2; // Ans: “I am Errorsea”

**Example 2:**

$e1 = “I am”;

$e2 = “ Errorsea”;

4e1 .= $e2; // Ans: “I am Errorsea”

**Conclusion**

These are the different types of operators used in PHP according to the requirements for providing users the better facilities. Users can take actions according to the result of different operators.

Enjoy Coding 🙂