PHP Operators
PHP Operators : An operator that takes one or more values (or expressions, in programming jargon) and produces another value (so that the construct itself becomes an expression). PHP operator is a symbol used to perform operations on operands.
For example, "1 + 2 = 3" in this expression '+'' is an operator. It takes two values 1 and 2, performs addition operation on them to give 3.
PHP language supports following type of operators :
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
5. Bitwise Operators
6. String Operators
7. Array Operators
8. Conditional Assignment Operators
PHP Arithmetic Operators
Operator | Name | Description | Syntax | Example |
---|---|---|---|---|
+ | Addition | Adds two operands | $A + $B | A + B will give 30 |
- | Subtraction | Subtracts second operand from the first | $A - $B | A - B will give -10 |
* | Multiplication | Multiply both operands | $A * $B | A * B will give 200 |
/ | Division | Divide numerator by de-numerator | $B / $A | B / A will give 2 |
% | Modulus | Modulus Operator and remainder of after an integer division | $B % $A | B % A will give 0 |
** | Exponentiation | Exponentiation Operator is used for raising the power | $A ** $B | Result of raising $A to the $B'th power |
++ | Increment operator | Increment operator, increases integer value by one | $A++ | A++ will give 11 |
-- | Decrement operator | Decrement operator, decreases integer value by one | $A-- | A-- will give 9 |
PHP Assignment Operators
Operator | Same as | Description | Example |
---|---|---|---|
A = B | A = B | Simple assignment operator, Assigns values from right side operands to left side operand | C = A + B will assign value of A + B into C |
A += B | C = A + B | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | A += B is equivalent to C = A + B |
A -= B | C = A - B | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | A -= B is equivalent to C = A - B |
A *= B | C = A * B | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | A *= B is equivalent to C = A * B |
A /= B | C = A / B | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand | A /= B is equivalent to C = A / B |
A %= B | C = A % B | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | A %= B is equivalent to C = A % B |
PHP Comparison Operators
Operator | Name | Description | Example |
---|---|---|---|
== | Equal | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
=== | Identical | Checks if the value of two operands are equal or not (should be with same type), if yes then condition becomes true. | (A === B) is not true. |
!= | Not equal | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true | (A != B) is true. |
<> | Not equal | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true | (A <> B) is true. |
!== | Not identical | Checks if the value of two operands are equal or not (they are not of the same type), if yes then condition becomes true. | (A !== B) is true. |
> | Greater than | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Less than | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Greater than or equal to | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true | (A >== B) is not true. |
<= | Less than or equal to | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true | (A <= B) is true. |
<=> | Spaceship | Returns an integer less than, equal to, or greater than zero, depending on if $A is less than, equal to, or greater than $B. Introduced in PHP 7. | (A <=> B) is -1. |
PHP Logical Operators
Operator | Name | Description | Example |
---|---|---|---|
and | AND | Called Logical AND operator. If both the operands are true then condition becomes true. | (A and B) is true. |
or | OR | Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. | (A or B) is true. |
&& | AND | Called Logical AND operator. If both the operands are non zero then condition becomes true. | (A && B) is true. |
|| | OR | Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. | (A || B) is true. |
! | NOT | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is false. |
PHP Bitwise Operators
Operator | Name | Description | Example |
---|---|---|---|
& | AND | Bits that are 1 in both $A and $B are set to 1, otherwise 0. | $A & $B |
| | OR | Bits that are 1 in either $A or $B are set to 1 | $A | $B |
^ | Xor (Exclusive or) | Bits that are 1 in either $A or $B are set to 0. | $A ^ $B |
~ | Not | Bits that are 1 set to 0 and bits that are 0 are set to 1 | ~$A |
<< | Shift left | Left shift the bits of operand $A $B steps | $A << $B |
>> | Shift right | Right shift the bits of $A operand by $B number of places | $A >> $B |
PHP String Operators
Operator | Name | Description | Example |
---|---|---|---|
. | Concatenation | Concatenate both $A and $B | $A . $B |
.= | Concatenation assignment | First concatenate $A and $B, then assign the concatenated string to $A, e.g. $A = $A . $B | $A .= $B |
PHP Array Operators
Operator | Name | Example | Description |
---|---|---|---|
+ | Union | $A + $B | Union of $A and $B |
== | Equality | $A == $B | Return TRUE if $A and $B have same key/value pair |
!= | Inequality | $A != $B | Return TRUE if $A is not equal to $B |
=== | Identity | $A === $B | Return TRUE if $A and $B have same key/value pair of same type in same order |
!== | Non-Identity | $A !== $B | Return TRUE if $A is not identical to $B |
<> | Inequality | $A <> $B | Return TRUE if $A is not equal to $B |
PHP Conditional Assignment Operators
Operator | Name | Example | Result |
---|---|---|---|
?: | Ternary | $x = expr1 ? expr2 : expr3 | Returns the value of $x. The value of $x is expr2 if expr1 = TRUE. The value of $x is expr3 if expr1 = FALSE |
?? | Null coalescing | $x = expr1 ?? expr2 | $A | $B |