Operators are used for performing operations between variables or values. Almost all languages provide a wide array of operators for mathematical values, strings, arrays and more.

PHP 8 is loaded with useful operators.

Below are the different kinds of operators that PHP 8 has to offer us.

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

### Arithmetic operators

Arithmetic operators perform a calculation between two or more numeric values. Below we will show these operators and use them in combination with variables.

Notation | Operator Name | Description |

+$x | Identity | Conversion of $x to int or float as appropriate. |

-$x | Negation | Opposite of $x. |

$x + $y | Addition | Sum of $x and $y. |

$x – $y | Subtraction | Difference of $x and $y. |

$x * $y | Multiplication | Product of $x and $y. |

$x / $y | Division | Quotient of $x and $y. |

$x % $y | Modulo | Remainder of $x divided by $y. |

$x ** $y | Exponentiation | Result of raising $x to the $y’th power. |

You can practice with the above operators with two or more variables.

One of the operators mentioned above which is not that straight forward is the modulo operator. Below some examples of this operator in action.

```
<?php
echo (7 % 3); // output 1
echo (7 % -3); // outputs 1
echo (-7 % 3); // outputs -1
echo (-7 % -3); // outputs -1
?>
```

### Assignment Operators

Assignment operators are used for assigning a value to a variable. See the following table for more detail.

Notation | Operation | Operation Name |

$x += $y | $z = $x + $y | Addition |

$x -= $y | $z = $x – $y | Subtraction |

$x *= $y | $z = $x * $y | Multiplication |

$x /= $y | $z = $x / $y | Division |

$x %= $y | $z = $x % $y | Modulus |

$x **= $y | $z = $x ** $y | Exponentiation |

You can also assign a value by reference using assignment operators. In this case the actual value is not assigned, but the address of the value is assigned to the variable where its values is stored in the memory.

### Comparison Operators

Comparison operators are important for PHP developers, so we will try to explain comparison operators in this section. Comparison operators perform comparisons between the left-hand side value and right-hand-side value. The value. however, can be either numeric or non-numeric.

$x == $y | Equal | true if $x is equal to $y after type juggling. |

$x === $y | Identical | true if $x is equal to $y, and they are of the same type. |

$x != $y | Not equal | true if $x is not equal to $y after type juggling. |

$x <> $y | Not equal | true if $x is not equal to $y after type juggling. |

$x !== $y | Not identical | true if $x is not equal to $y, or they are not of the same type. |

$x < $y | Less than | true if $x is strictly less than $y. |

$x > $y | Greater than | true if $x is strictly greater than $y. |

$x <= $y | Less than or equal to | true if $x is less than or equal to $y. |

$x >= $y | Greater than or equal to | true if $x is greater than or equal to $y. |

$x <=> $y | Spaceship | An int less than, equal to, or greater than zero when $x is less than, equal to, or greater than $y, respectively. |

In the previous versions of PHP, if a string is compared with a numeric value or numeric string, the result is unexpected as the string is converted into a number first. This problem is now solved in PHP 8 and produces the expected output.

### Increment/Decrement Operators

Increment and decrement operators increase or decrease a value by one respectively. They only work with numbers and strings. Increment/decrement operators do not work with boolean variables and nor with arrays.

#### pre-increment and post-increment

**Pre-increment**

Increments *$x* by one and then returns *$x*.

```
<?php
$x=5;
echo ++$x; // outputs 6
?>
```

**Post-increment**

Returns *$x*, then increments *$x* by one. It is written as $x++

```
<?php
$x=5;
echo $x++; // outputs 5
echo $x; // outputs 6
?>
```

### pre-decrement and post-decrement in PHP

**pre-decrement**

Decrements *$x* by one, then returns *$x*.

```
<?php
$x=5;
echo --$x; // outputs 4
?>
```

**Post-decrement**

Returns *$x*, then decrements *$x* by one.

```
<?php
$x=5;
echo $x--; // outputs 4
?>
```

### Logical Operators

Logical operators output either true or false. The following table illustrates these operators.

$x and $y | And | true if both $x and $y are true. |

$x or $y | Or | true if either $x or $y is true. |

$x xor $y | Xor | true if either $x or $y is true, but not both. |

! $x | Not | true if $x is not true. |

$x && $y | And | true if both $x and $y are true. |

$x || $y | Or | true if either $x or $y is true. |

### String Operators

There are two string operators in PHP.

- Concatenation Operators ( . )
- Concatenation Assignment Operators ( . )

The concatenation operator joins the left string with the right string. The concatenation assignment operator joins the right argument with the argument on the left side.

```
<?php
$x = "Hey ";
$y = $a . "PHP!"; // now $x contains "Hey PHP!"
$x = "Hey ";
$y .= "PHP!"; // now $x contains "Hey PHP!"
?>
```

### Array Operators

Array operators allows to add or compare two or more arrays. PHP 8 contains the following array operators.

$x + $y | Union | Union of $x and $y. |

$x == $y | Equality | true if $x and $y have the same key/value pairs. |

$x === $y | Identity | true if $x and $y have the same key/value pairs in the same order and of the same types. |

$x != $y | Inequality | true if $x is not equal to $y. |

$x <> $y | Inequality | true if $x is not equal to $y. |

$x !== $y | Non-identity | true if $x is not identical to $y. |

Two arrays are equal if they have the same keys and values corresponding to them. However, the hard comparison *=== *also checks for their types.

```
<?php
$x = array("a", "b");
$y = array(1 => "a", "0" => "b");
var_dump($x == $y); // bool(true)
var_dump($x === $y); // bool(false)
?>
```

### Conditional Assignment Operators

We have three conditional assignment operators in PHP: && the logical and operator || the logical OR operator and ?: the *ternary operator*.

A ternary operator is a short form of if..else statement. It returns the value based on the condition.

```
<?php
$x = $exp1 ? $exp2 : $exp3;
// The value of $x will be assigned $exp2 if the $exp1 is true, else it will be assigned $exp3
?>
```

**Null Coalescing Operator**

Similar to ternary but this operator checks for NULL

```
<?php
$x = $var1 ?? $var2 : $var3;
// The value of $x will be assigned to $var2
// if $var1 is NULL, else it will be assigned $var3
?>
```

Other than these operators discussed above we have the` operator known as an *execution operator* and it helps in running the shell commands.

In case you want to research operators in PHP more you can check out the official reference to PHP 8 operators.