An operator takes one or more values and produces a new one: $a + $b adds, $a . $b joins strings, $a && $b is a logical AND, $a === $b compares, $i++ increments.
/ returns a float unless two integers divide evenly: 17 / 5 is 3.4, 10 / 5 is the integer 2. When you want the whole-number part only, use intdiv(). Rounding is covered on the [math page](/docs/php/math).
Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | Division | 7 / 2 | 3.5 |
% | Modulo (remainder) | 7 % 2 | 1 |
** | Exponent | 7 ** 2 | 49 |
-$a | Negation | -7 | -7 |
Modulo is useful for "every Nth item" and even/odd checks. Its result takes the sign of the left side, and dividing by zero throws an error you can catch:
Assignment operators
= stores a value. Every arithmetic and string operator has a combined form that updates a variable in place.
??= belongs to the null coalescing operator family. It assigns only when the variable is unset or null.
Increment and decrement: ++ and --
$i++ returns the current value, then adds 1. ++$i adds 1, then returns the new value. The difference only shows when you use the result in the same expression.
The letter trick is how spreadsheet column names (A, B, ... Z, AA) are often generated.
Logical operators: &&, ||, ! and xor
| Operator | True when |
|---|---|
$a && $b | both are true |
$a || $b | at least one is true |
!$a | $a is false |
$a xor $b | exactly one is true |
$a and $b, $a or $b | same as && and ||, with lower precedence |
&& and || short-circuit: the right side is not evaluated when the left side already decides the result. That is what makes isset($user) && $user->isAdmin() safe.
and vs &&: the precedence trap
and and or bind more loosely than =, so in an assignment they apply after the value is stored. This is a classic bug:
The low precedence is useful in one idiom, $file = fopen($path, 'r') or die('Cannot open');, where you want the assignment to happen first. Everywhere else, use && and ||.
String operators: . and .=
PHP has two string operators: . joins two strings ("Grace" . " " . "Hopper") and .= appends to a variable ($html .= "<li>"). + never joins strings: "3" + "4" is the number 7, while "3" . "4" is the string "34". The strings page covers interpolation and quotes.
Comparison and the spaceship operator <=>
== compares after converting types, === also checks the type, and <=> returns -1, 0 or 1, which is exactly what sort callbacks need. The details and the full loose comparison table are on the comparison operators page.
Array operators
+ on arrays is a union: keys from the left array win and only missing keys are added from the right. That surprises people who expect it to append. == checks the same key/value pairs, === also requires the same order and types.
To append one list to another, use array_merge() or the spread syntax [...$a, ...$b].
Operator precedence
When an expression mixes operators, PHP applies them in a fixed order. The ones that trip people up, with what PHP 8.3 prints:
echo 2 + 3 * 4; // 14: * before +
echo (2 + 3) * 4; // 20
echo 2 ** 3 ** 2; // 512: right to left, 2 ** 9
echo -2 ** 2; // -4: ** before unary minus
echo "Sum: " . 2 + 3; // Sum: 5, since PHP 8 + runs before .
When in doubt, add parentheses. They cost nothing and the next person reading the line does not have to remember the table.
Frequently Asked Questions
What is the difference between and and && in PHP?
Both are logical AND, but and has lower precedence than =. So $x = true and false; stores true in $x (the assignment happens first), while $x = true && false; stores false. Use && and || in expressions and keep and/or out of assignments.
What does % do in PHP?
% is the modulo operator: it returns the remainder of an integer division, so 10 % 3 is 1. The result takes the sign of the left operand (-7 % 3 is -1). For floats use fmod(), and % 0 throws a DivisionByZeroError.
How do I raise a number to a power in PHP?
Use the ** operator: 2 ** 10 is 1024. It is right-associative, so 2 ** 3 ** 2 is 2 ** 9 = 512, and it binds tighter than unary minus, so -2 ** 2 is -4. The function pow(2, 10) does the same.
What is the difference between $i++ and ++$i in PHP?
Both add 1 to $i. $i++ (post-increment) returns the old value and then increments; ++$i (pre-increment) increments first and returns the new value. On a line of its own, like a for loop step, there is no difference.