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 ﬂoats.
The most common PHP operators are:
- Assignment operators
- Arithmetic operators
- Combined operators
- Comparison operators
- Logical operators
- Ternary 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 "=".
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.
$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.
You can combine an arithmetic operator with the assignment operator to form a combined operator. Combined operators are:
++: 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.
$a = 3;
$b = $a++; // $b is 3, $a is 4
$b = ++$a; // $a and $b are 5
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).
- == (equal to)
- != (not equal to)
- === (identical)
- !== (not identical)
- > (greater than)
- >= (greater than or equal to)
- < (less than)
- <= (less than or equal to)
- <=> (spaceship)
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.
- || (or)
- && (and)
- ! (not)