PHP - Operators
Operators are used to perform operation on a single operand or two operands. Operators in PHP can be categorized as follows:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- Bitwise operators
- Miscellaneous operators
PHP Arithmetic operators
Arithmetic operators are used to perform arithmetic operations on two operands.
Operator | Name | Description |
---|---|---|
+ | Addition | Add two values |
- | Subtraction | Subtract one value from another |
* | Multiplication | Multiply two values |
/ | Division | Divide one value by another |
** | Exponent / Power | Returns first operand raised to the power of second operand |
% | Modulo | Returns remainder of integer division |
Example
PHP Assignment operators
Assignment operators are used to assign values of right hand side expression to left hand side operand.
Operator | Expression | Equivalent to | Example |
---|---|---|---|
= | a = 5 | a = 5 | Example |
+= | a += b | a = a + b | |
-= | a -= b | a = a - b | |
*= | a *= b | a = a * b | |
/= | a /= b | a = a / b | |
**= | a **= b | a = a ** b | |
%= | a %= b | a = a % b | |
&= | a &= b | a = a & b | More Info |
|= | a |= b | a = a | b | More Info |
^= | a ^= b | a = a ^ b | More Info |
>>= | a >>= b | a = a >> b | More Info |
<<= | a <<= b | a = a << b | More Info |
PHP Comparison operators
Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.
Operator | Description |
---|---|
== | Equal: Checks the values of two operands and returns true if they are same. |
!= | Not equal: Checks the values of two operands and returns true if they are not same. |
<> | |
=== | Identical: Checks the values and datatypes of two operands and returns true if they are same. |
!== | Not identical: Checks the values and datatypes of two operands and returns true if they are not same. |
> | Greater than: Checks the values of two operands and returns true if the value of first operand is greater than the value of second operand. |
< | Less than: Checks the values of two operands and returns true if the value of first operand is less than the value of second operand. |
>= | Greater than or equal to: Checks the values of two operands and returns true if the value of first operand is greater than or equal to the value of second operand. |
<= | Less than or equal to: Checks the values of two operands and returns true if the value of first operand is less than or equal to the value of second operand. |
<=> | Spaceship: Checks the values of two operands and returns values based on the values of two operands:
|
Example
PHP Increment/Decrement operators
Increment and decrement operators are used to increase and decrease the value of variable.
Operator | Name | Description | Example |
---|---|---|---|
++$x | Pre-increment | Increases the value of $x by 1, then returns $x. | Example |
$x++ | Post-increment | Returns $x, then increases the value of $x by 1. | |
--$x | Pre-decrement | Decreases the value of $x by 1, then returns $x. | Example |
$x-- | Post-decrement | Returns $x, then decreases the value of $x by 1. |
PHP Logical operators
Logical operators are used to combine two or more conditions.
Operator | Name | Description |
---|---|---|
and | AND | Returns true when all conditions are true |
&& | ||
or | OR | Returns true when any of the conditions is true |
|| | ||
xor | Exclusive OR | Returns true when any of the conditions is true, but not both |
! | NOT | Returns true when the given conditions is not true |
More Info
PHP Bitwise operators
Bitwise operators are used to perform bitwise operations on two operands.
Operator | Name | Description | More Info |
---|---|---|---|
& | AND | Returns 1 if both bits at the same position in both operands are 1, else returns 0 | More Info |
| | OR | Returns 1 if one of two bits at the same position in both operands is 1, else returns 0 | More Info |
^ | XOR | Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0 | More Info |
~ | NOT | Reverse all the bits | More Info |
>> | Right shift | The left operand is moved right by the number of bits present in the right operand | More Info |
<< | Left shift | The left operand value is moved left by the number of bits present in the right operand | More Info |
PHP Miscellaneous operators
The table below describes other operators supported by PHP:
Operator | Name | Example |
---|---|---|
?: | Ternary | $x = exp1 ? exp2 : exp3
|
?? | Null coalescing | $x = exp1 ?? exp2
|
PHP Operators Precedence
Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.
For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.
The following table lists the precedence and associativity of PHP operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.
Precedence | Operator | Description | Associativity |
---|---|---|---|
25 | clone | clone operator | NA |
new | new operator | ||
24 | ** | Exponentiation | Right to Left |
23 | ++a --a | Prefix increment, Prefix decrement | NA |
a++ a-- | Postfix increment, Postfix decrement | ||
+a -a | Unary plus, Unary minus | ||
~ | Bitwise NOT | ||
(int) (float) (string) (array) (object) (bool) | Type casting | ||
@ | Error control operator | ||
22 | instanceof | Type operator | Left to Right |
21 | ! | Logical NOT | NA |
20 | * / % | Multiplication, Division, Remainder | Left to Right |
19 | + - . | Addition, Subtraction, Concatenating operator (. prior to PHP 8.0.0) | |
18 | << >> | Bitwise left shift and right shift | |
17 | . | Concatenating operator (as of PHP 8.0.0) | |
16 | < <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal | non-associative |
15 | == != | Equality, Inequality | |
=== !== | Strict Equality, Strict Inequality | ||
<> <=> | Inequality, Spaceship operator | ||
14 | & | Bitwise AND and reference operator | Left to Right |
13 | ^ | Bitwise XOR | |
12 | | | Bitwise OR | |
11 | && | Logical AND | |
10 | || | Logical OR | |
9 | ?? | Null coalescing operator | Right to Left |
8 | a?b:c | Conditional (ternary) operator | Non-associative (Left to Right associative prior to PHP 8.0.0) |
7 | = | Direct assignment | Right to Left |
+= -= *= /= %= **= | Compound assignment by addition, subtraction, multiplication, division, remainder, and exponentiation | ||
.= ??= | Concatenating assignment operator, Null coalescing assignment operator | ||
<<= >>= | Compound assignment by Bitwise left shift and right shift | ||
&= ^= |= | Compound assignment by Bitwise AND, XOR and OR | ||
6 | yield from | yield from | NA |
5 | yield | yield | |
4 | |||
3 | and | Logical AND | Left to Right |
2 | xor | Logical XOR | |
1 | or | Logical OR |