PHP Operators
Operators are used to manipulate variables and expressions. A unary operator operates on a single variable or expression (the operand). For example, the ++ (increment) operator increases the value of its operand by one. A binary operator (the most commonly used kind of operator) operates on two operands. The + (addition) operator, for example, sums the value of its two operands. The ternary operator (?:) is a conditional operator used to select between two operands, depending on the value of a third. The main operators used in PHP are described below.
Symbol | Description | Usage | Result |
---|---|---|---|
+ | Addition | $n + $m | Sum of $n and $m |
- | Subtraction | $n - $m | Difference of $n and $m |
* | Multiplication | $n * $m | Product of $n and $m |
/ | Division | $n / $m | Quotient of $n and $m (note that a floating-point value is returned unless both operands are integers, and the first operand is divisible by the second) |
% | Modulus | $n % $m | Remainder of $n divided by $m (converts floating-point operands to integers by removing the decimal part before processing) |
Symbol | Description | Usage | Result |
---|---|---|---|
++ | Pre-increment | ++ $n | Increments $n by one, then returns $n |
++ | Post-increment | $n ++ | Returns $n, then increments $n by one |
-- | Pre-decrement | -- $n | Decrements $n by one, then returns $n |
-- | Post-decrement | $n -- | Returns $n, then decrements $n by one |
Symbol | Description | Usage | Result |
---|---|---|---|
= | Assignment | $n = $m | Sets value of $n to value of $m |
+= | Assignment by addition | $n += $m | Sets value of $n to value of $n + $m (same as $n = $n + $m) |
-= | Assignment by subtraction | $n -= $m | Sets value of $n to value of $n - $m (same as $n = $n - $m) |
*= | Assignment by multiplication | $n *= $m | Sets value of $n to value of $n * $m (same as $n = $n * $m) |
/= | Assignment by division | $n /= $m | Sets value of $n to value of $n / $m (same as $n = $n / $m) |
%= | Assignment by modulus | $n %= $m | Sets value of $n to value of $n % $m (same as $n = $n % $m) |
Symbol | Description | Usage | Result |
---|---|---|---|
== | Equal | $n == $m | TRUE if $n is equal to $m |
=== | Identical | $n === $m | TRUE if $n is equal to $m, and they are of the same type |
!= | Not equal | $n != $m | TRUE if $n is not equal to $m |
<> | Not equal | $n <> $m | TRUE if $n is not equal to $m |
!== | Not identical | $n !== $m | TRUE if $n is not equal to $m, or they are not of the same type |
< | Less than | $n < $m | TRUE if $n is less than $m |
> | Greater than | $n > $m | TRUE if $n is greater than $m |
<= | Less than or equal to | $n <= $m | TRUE if $n is less than or equal to $m |
>= | Greater than or equal to | $n >= $m | TRUE if $n is greater than or equal to $m |
Symbol | Description | Usage | Result |
---|---|---|---|
&& | AND | $n && $m | TRUE if both $n and $m are TRUE |
and | AND | $n and $m | TRUE if both $n and $m are TRUE |
|| | OR | $n || $m | TRUE if either $n or $m is TRUE |
or | OR | $n or $m | TRUE if either $n or $m is TRUE |
! | NOT | ! $n | TRUE if $n is not TRUE |
xor | Exclusive OR (XOR) | $n xor $m | TRUE if either $n or $m is TRUE (but not both) |
Symbol | Description | Usage | Result |
---|---|---|---|
& | Bitwise AND | $n & $m | Bits that are set in both $n and $m are set |
| | Bitwise OR | $n | $m | Bits that are set in either $n or $m are set |
^ | Bitwise XOR | $n ^ $m | Bits that are set in either $n or $m (but not in both) are set |
~ | Bitwise NOT | ~ $n | All bits in $n are inverted |
<< | Shift left | $n << $m | Shift the bits of $n to the left by $m places (bits shifted off the left-hand end are discarded, zeros are shifted in on the right. The sign bit is shifted out, so sign is not preserved). |
>> | Shift right | $n >> $m | Shift the bits of $n to the right by $m places (bits shifted off the right-hand end are discarded, and a copy of the sign bit is shifted in on the left, so sign is preserved. |
Operator precedence
Operator precedence determines the order in which expressions are evaluated. For example, in the statement x = 18 + 17 * 2, x evaluates to 52 and not 70 because the multiplication operator (*) has a higher precedence than the addition (+) operator. Parentheses can be used to change precedence if necessary. For example, if we re-write the statement to read x = (18 + 17) * 2, x does evaluate to 70 because expressions inside parentheses are always evaluated first. The table below lists the common operators described above in order of precedence from highest to lowest (operators on the same line have equal precedence).
Operator(s) | Description |
---|---|
++, -- | Increment and decrement operators |
~ | Bitwise NOT |
! | Logical NOT |
*, /, % | Multiplication, division and modulus |
+, - | Addition and subtraction |
<<, >> | Bitwise shift left and shift right |
<, <=, >, >=, <> |
Comparison operators (less than, less than or equal, greater than, greater than or equal, not equal) |
==, !=, ===, !== | Comparison operators (equal, not equal, identical, not identical) |
& | Bitwise AND |
^ | Bitwise XOR |
| | Bitwise OR |
&& | Logical AND |
|| | Logical OR |
=, +=, -=, *=, /=, %= | Assignment operators (plain assignment, assignment by addition, assignment by subtraction, assignment by multiplication, assignment by division, assignment by modulus) |
? : | The ternary operator |
and | Logical AND |
xor | Logical XOR |
or | Logical OR |