Operators are elements that take some expressions, operands, and perform actions on them to get a result. An expression is almost anything that has a value. Variables, numbers, or text are examples of expressions. Operators expect expressions of a specific type, for example, arithmetic operators expect either integers or floats.

The most common PHP operators are:

  1. Assignment operators
  2. Arithmetic operators
  3. Combined operators
  4. Comparison operators
  5. Logical operators
  6. Ternary operators

Assignment Operators

The assignment operator assigns the result of an expression to a variable. The basic assignment operator in PHP is "=". This means that the operand to the left of "=" gets set to the value to the right of "=".

Arithmetic Operators

Addition, subtraction, multiplication, and division (+, -, *, and /) do as their names say. Modulus (%) gives the remainder of the division of two operands. Exponentiation (**) raises the first operand to the power of the second. Negation (-) negates the operand.

  1. +
  2. -
  3. *
  4. /
  5. %

Example

<?php
$a = 10;
$b = 3;
var_dump($a + $b); // 13
var_dump($a - $b); // 7
var_dump($a * $b); // 30
var_dump($a / $b); // 3.333333...
var_dump($a % $b); // 1
var_dump($a ** $b); // 1000
var_dump(-$a); // -10

PHP does multiplication and division first, followed by addition and subtraction. If other considerations are equal, PHP goes from left to right. You can change the order in which the arithmetic is performed by using parentheses.

Combined Operators

You can combine an arithmetic operator with the assignment operator to form a combined operator. Combined operators are:

  1. ++
  2. --
  3. +=
  4. -=
  5. *=
  6. /=
  7. %=

++: This operator on the left of the variable will increase the variable by 1, and then return the result. On the right, it will return the content of the variable, and after that increase it by 1.

--: This operator works the same as ++ but decreases the value by 1 instead of increasing by 1.

Example

<?php
$a = 3;
$b = $a++; // $b is 3, $a is 4
var_dump($a, $b);
$b = ++$a; // $a and $b are 5
var_dump($a, $b);

Comparison Operators

Comparison operators are one of the most used groups of operators. They take two operands and compare them, returning the result of the comparison usually as a Boolean, that is, true or false. 

There are four comparisons that are very intuitive: < (less than), <= (less or equal to), > (greater than), and >= (greater than or equal to). There is also the special operator <=> (spaceship) that compares both the operands and returns an integer instead of a Boolean.

There are comparison operators to evaluate if two expressions are equal or not. The == (equals) operator evaluates two expressions after type juggling, that is, it will try to transform both expressions to the same type, and then compare them. Instead, the === (identical) operator evaluates two expressions without type juggling, so even if they look the same, if they are not of the same type, the comparison will return false. The same applies to != or <> (not equal to) and !== (not identical).

  1. == (equal to)
  2. != (not equal to)
  3. === (identical)
  4. !== (not identical)
  5. > (greater than)
  6. >= (greater than or equal to)
  7. < (less than)
  8. <= (less than or equal to)
  9. <=> (spaceship)

Logical Operators

Logical operators apply a logic operation, also known as a binary operation, to its operands, returning a Boolean response. The most used ones are ! (not), && (and), and || (or). && will return true only if both operands evaluate to true. || will return true if any or both of the operands are true. ! will return the negated value of the operand, that is, true if the operand is false or false if the operand is true.

  1. || (or)
  2. && (and)
  3. ! (not)

Ternary Operators

  1. Shorthand for If-Else - ?: