Author: | |
Website: | |
Page title: | |
URL: | |
Published: | |
Last revised: | |
Accessed: |
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.
Arithmetic operators are used with numeric values to perform operations such as addition, subtraction, multiplication, division, and so on. The term binary refers to the fact that two operands are involved.
Symbol | Description | Usage | Result |
---|---|---|---|
+ | Addition | $m + $n | Sum of $m and $n |
- | Subtraction | $m - $n | Difference of $m and $n |
* | Multiplication | $m * $n | Product of $m and $n |
/ | Division | $m / $n | Quotient of $m and $n (note that a floating-point value is returned unless both operands are integers, and the first operand is divisible by the second) |
% | Modulus | $m % $n | Remainder of $m divided by $n (converts floating-point operands to integers by removing the decimal part before processing) |
** | Exponentiation | $m ** $n | Result of raising $m to the $nth power |
Unary arithmetic operators are used with numeric values to perform operations such as incrementing and decrementing values. The term unary refers to the fact that only one operand is involved.
Symbol | Description | Usage | Result |
---|---|---|---|
++ | Pre-increment | ++ $m | Increments $m by one, then returns $m |
++ | Post-increment | $m ++ | Returns $m, then increments $m by one |
-- | Pre-decrement | -- $m | Decrements $m by one, then returns $m |
-- | Post-decrement | $m -- | Returns $m, then decrements $m by one |
Assignment operators are used with numeric values to assign a value to a variable. The basic assignment operator (=) simply assigns the value of the right-hand operand to the left-hand operand. Other assignment operators first perform some mathematical operation involving both operands, such as adding the two operands together, and then assigns the result of that operation to the left-hand operand.
Symbol | Description | Usage | Result |
---|---|---|---|
= | Assignment | $m = $n | Sets value of $m to value of $n |
+= | Assignment by addition | $m += $n | Sets value of $m to value of $m + $n (same as $m = $m + $n) |
-= | Assignment by subtraction | $m -= $n | Sets value of $m to value of $m - $n (same as $m = $m - $n) |
*= | Assignment by multiplication | $m *= $n | Sets value of $m to value of $m * $n (same as $m = $m * $n) |
/= | Assignment by division | $m /= $n | Sets value of $m to value of $m / $n (same as $m = $m / $n) |
%= | Assignment by modulus | $m %= $n | Sets value of $m to value of $m % $n (same as $m = $m % $n) |
Comparison operators are used to compare two values - for example two string values or two numeric values – to determine whether or not they have a specific relationship. The result of the comparison is always a Boolean value (TRUE or FALSE).
Symbol | Description | Usage | Result |
---|---|---|---|
== | Equal | $m == $n | TRUE if $m is equal to $n |
=== | Identical | $m === $n | TRUE if $m is equal to $n, and they are of the same type |
!= | Not equal | $m != $n | TRUE if $m is not equal to $n |
<> | Not equal | $m <> $n | TRUE if $m is not equal to $n |
!== | Not identical | $m !== $n | TRUE if $m is not equal to $n, or they are not of the same type |
< | Less than | $m < $n | TRUE if $m is less than $n |
> | Greater than | $m > $n | TRUE if $m is greater than $n |
<= | Less than or equal to | $m <= $n | TRUE if $m is less than or equal to $n |
>= | Greater than or equal to | $m >= $n | TRUE if $m is greater than or equal to $n |
<=> | Spaceship | $m <=> $n | Returns an integer less than, equal to, or greater than zero, depending on whether $m is less than, equal to, or greater than $n |
Logical operators are used to test for the existence of a specific logic state, such as whether two expressions both evaluate to TRUE. The result of the test is a Boolean value (TRUE or FALSE).
Symbol | Description | Usage | Result |
---|---|---|---|
&& | AND | $m && $n | TRUE if both $m and $n are TRUE |
and | AND | $m and $n | TRUE if both $m and $n are TRUE |
|| | OR | $m || $n | TRUE if either $m or $n is TRUE |
or | OR | $m or $n | TRUE if either $m or $n is TRUE |
! | NOT | ! $m | TRUE if $m is not TRUE |
xor | Exclusive OR (XOR) | $m xor $n | TRUE if either $m or $n is TRUE (but not both) |
Bitwise operators allow evaluation and manipulation of specific bits within an integer.
Symbol | Description | Usage | Result |
---|---|---|---|
& | Bitwise AND | $m & $n | Bits that are set in both $m and $n are set |
| | Bitwise OR | $m | $n | Bits that are set in either $m or $n are set |
^ | Bitwise XOR | $m ^ $n | Bits that are set in either $m or $n (but not in both) are set |
~ | Bitwise NOT | ~ $m | All bits in $m are inverted |
<< | Shift left | $m << $n | Shift the bits of $m to the left by $n places (bits shifted off the left-hand end are discarded, and zeros are shifted in on the right. The sign bit is shifted out, so sign is not preserved). |
>> | Shift right | $m >> $n | Shift the bits of $m to the right by $n 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. |
PHP has just two operators that are specifically intended for use with strings - the concatenation operator and the concatenation assignment operator (these work in similar fashion to + and += for numeric values).
Symbol | Description | Usage | Result |
---|---|---|---|
. | Concatenation | $str1 . str2 | Concatenation of $str1 and $str2 |
.= | Concatenation assignment | $str1 .= $str2 | Appends $str2 to $str1 |
Array operators are essentially special comparison operators for use with array variables.
Symbol | Description | Usage | Result |
---|---|---|---|
+ | Union | $m + $n | Union of $m and $n |
== | Equality | $m == $n | Returns TRUE if $m and $n have the same key/value pairs |
=== | Identity | $m === $n | Returns true if $m and $n have the same key/value pairs in the same order and of the same types |
!= | Inequality | $m != $n | Returns true if $m is not equal to $n |
<> | Inequality | $m <> $n | Returns true if $m is not equal to $n |
!== | Non-identity | $m !== $n | Returns true if $m is not identical to $n |
A conditional assignment operator will return one of two possible values, depending on whether or not the specified condition has been met.
Symbol | Description | Usage | Result |
---|---|---|---|
? : | Ternary | $m = expr1 ? expr2 : expr3 | Returns the value of $m The value of $m is expr2 if expr1 = TRUE The value of $m is expr3 if expr1 = FALSE |
?? | Null coalescing | $m = expr1 ?? expr2 | Returns the value of $m The value of $m is expr1 if expr1 exists, and is not NULL If expr1 does not exist, or is NULL, the value of $m is expr2 |
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 |
---|---|
** | Exponentiation |
++, -- | Increment and decrement operators |
~ | Bitwise NOT |
! | Logical NOT |
*, /, % | Multiplication, division and modulus |
+, - | Addition and subtraction |
<<, >> | Bitwise shift left and shift right |
: | String concatenation |
<, <=, >, >= | Comparison operators (less than, less than or equal, greater than, greater than or equal) |
==, !=, ===, !==, <>, <=> | Comparison operators (equal, not equal, identical, not identical, not equal, spaceship) |
& | 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, string concatenation assignment) |
? : | The ternary operator |
and | Logical AND |
xor | Logical XOR |
or | Logical OR |