PHP Operators
Operators are symbols that take some expressions, operands, and perform mathematical or logical 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.
Types of PHP Operators
- Assignment operators
- Arithmetic operators
- Combined operators
- Comparison operators
- Logical operators
- Ternary operators
1. Assignment Operators
The assignment operator assigns a value 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 "=".
$x = 1; // assignment
2. Arithmetic Operators
The arithmetic operators include the four basic arithmetic operations - Addition, subtraction, multiplication, and division (+, -, *, and /) and the modulus operator (%) that gives the remainder of the division of two operands.
$x = 4 + 2; // 6 addition
$x = 4 - 2; // 2 subtraction
$x = 4 * 2; // 8 multiplication
$x = 4 / 2; // 2 division
$x = 4 % 2; // 0 modulus (division remainder)
Exponentiation operator (**) raises the first operand to the power of the second. Negation (-) negates the operand.
$x = 4 ** 2; // 16 // exponentiation
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.
3. Combined Assignment Operators
A common use of the assignment and arithmetic operators is to operate on a variable and then to save the result back into that same variable. These operations can be shortened with the combined assignment operators.
You can combine an arithmetic operator with the assignment operator to form a combined operator. Combined operators are:
- ++
- --
- +=
- -=
- *=
- /=
- %=
$x += 5; // $x = $x+5;
$x -= 5; // $x = $x-5;
$x *= 5; // $x = $x*5;
$x /= 5; // $x = $x/5;
$x %= 5; // $x = $x%5;
Increment and Decrement Operators
Another common operation is to increment or decrement a variable by one.
-
++: 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.
$x++; // $x += 1;
$x--; // $x -= 1;
Both of these operators can be used either before or after a variable.
$x++; // post-increment
$x--; // post-decrement
++$x; // pre-increment
--$x; // pre-decrement
The result on the variable is the same whichever is used. The difference is that the post-operator returns the original value before it changes the variable, whereas the pre-operator changes the variable first and then returns the value.
$x = 5; $y = $x++; // $x=6, $y=5
$x = 5; $y = ++$x; // $x=6, $y=6
4. Comparison Operators
The comparison operators compare two values and return either true or false. They are mainly used to specify conditions, which are expressions that evaluate to either 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.
- == (equal to)
- != (not equal to)
- === (identical)
- !== (not identical)
- > (greater than)
- >= (greater than or equal to)
- < (less than)
- <= (less than or equal to)
- <=> (spaceship)
The strict equality operators, === and !==, are used for comparing both type and value. These are necessary because the regular "equal to" (==) and "not equal to" (!=) operators automatically perform a type conversion before they compare the operands. For example,
$x = (1 == "1"); // true (same value)
$x = (1 === "1"); // false (different types)
PHP 7 added a new comparison operator called the spaceship operator (<=>). It compares two values and returns 0 if both values are equal; 1 if the value on the left side is greater; and -1 if the value on the right side is greater.
$x = 1 <=> 1; // 0 (1 == 1)
$x = 1 <=> 2; //-1 (1 < 2)
$x = 3 <=> 2; // 1 (3 > 2)
5. Logical Operators
The logical operators are often used together with the comparison operators.
The and operator returns true if both the operands evaluate to true. The or operator returns true if any or both of the operands are true. The not operator returns the negated value of the operand, that is, true if the operand is false or false if the operand is true.
- || (or)
- && (and)
- ! (not)
For example,
$x = (true && false); // false // logical and
$x = (true || false); // true // logical or
$x = !(true); // false // logical not