Operators are used to perform operations on variables and values.
$num=10+20;//+ is the operator and 10,20 are operands
In the above example, + is the binary + operator, 10 and 20 are operands and $num is variable.
PHP divides the operators in the following groups:
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Incrementing/Decrementing Operators
- Logical Operators
- String Operators
- Array Operators
- Conditional assignment operators
We can also categorize operators on behalf of operands. They can be categorized in 3 forms:
- Unary Operators: works on single operands such as ++, — etc.
- Binary Operators: works on two operands such as binary +, -, *, / etc.
- Ternary Operators: works on three operands such as “?:”.
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
|+||Addition||$a + $b||Sum of operands|
|–||Subtraction||$a – $b||Difference of operands|
|*||Multiplication||$a * $b||Product of operands|
|/||Division||$a / $b||Quotient of operands|
|%||Modulus||$a % $b||Remainder of operands|
|**||Exponentiation||$a ** $b||$a raised to the power $b|
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is “=”. It means that the left operand gets set to the value of the assignment expression on the right.
|=||Assign||$a = $b||The value of right operand is assigned to the left operand.|
|+=||Add then Assign||$a += $b||Addition same as $a = $a + $b|
|-=||Subtract then Assign||$a -= $b||Subtraction same as $a = $a – $b|
|*=||Multiply then Assign||$a *= $b||Multiplication same as $a = $a * $b|
|/=||Divide then Assign|
|$a /= $b||Find quotient same as $a = $a / $b|
|%=||Divide then Assign|
|$a %= $b||Find remainder same as $a = $a % $b|
PHP Bitwise Operators
The bitwise operators are used to perform bit-level operations on operands. These operators allow the evaluation and manipulation of specific bits within the integer.
|&||And||$a & $b||Bits that are 1 in both $a and $b are set to 1, otherwise 0.|
||||Or (Inclusive or)||$a | $b||Bits that are 1 in either $a or $b are set to 1|
|^||Xor (Exclusive or)||$a ^ $b||Bits that are 1 in either $a or $b are set to 0.|
|~||Not||~$a||Bits that are 1 set to 0 and bits that are 0 are set to 1|
|<<||Shift left||$a << $b||Left shift the bits of operand $a $b steps|
|>>||Shift right||$a >> $b||Right shift the bits of $a operand by $b number of places|
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
|==||Equal||$a == $b||Return TRUE if $a is equal to $b|
|===||Identical||$a === $b||Return TRUE if $a is equal to $b, and they are of same data type|
|!==||Not identical||$a !== $b||Return TRUE if $a is not equal to $b, and they are not of same data type|
|!=||Not equal||$a != $b||Return TRUE if $a is not equal to $b|
|<>||Not equal||$a <> $b||Return TRUE if $a is not equal to $b|
|<||Less than||$a < $b||Return TRUE if $a is less than $b|
|>||Greater than||$a > $b||Return TRUE if $a is greater than $b|
|<=||Less than or equal to||$a <= $b||Return TRUE if $a is less than or equal $b|
|>=||Greater than or equal to||$a >= $b||Return TRUE if $a is greater than or equal $b|
|<=>||Spaceship||$a <=>$b||Return -1 if $a is less than $b|
Return 0 if $a is equal $b
Return 1 if $a is greater than $b
PHP Increment / Decrement Operators
- The PHP increment operators are used to increment a variable’s value.
- The PHP decrement operators are used to decrement a variable’s value.
|++||Increment||++$a||Increment the value of $a by one, then return $a|
|$a++||Return $a, then increment the value of $a by one|
|—||decrement||–$a||Decrement the value of $a by one, then return $a|
|$a–||Return $a, then decrement the value of $a by one|
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
|and||And||$a and $b||Return TRUE if both $a and $b are true|
|Or||Or||$a or $b||Return TRUE if either $a or $b is true|
|xor||Xor||$a xor $b||Return TRUE if either $ or $b is true but not both|
|!||Not||! $a||Return TRUE if $a is not true|
|&&||And||$a && $b||Return TRUE if either $a and $b are true|
|||||Or||$a || $b||Return TRUE if either $a or $b is true|
PHP String Operators
PHP has two operators that are specially designed for strings.
|.||Concatenation||$a . $b||Concatenate both $a and $b|
|.=||Concatenation and Assignment||$a .= $b||First concatenate $a and $b, then assign the concatenated string to $a, e.g. $a = $a . $b|
PHP Array Operators
The PHP array operators are used to compare arrays.
|+||Union||$a + $y||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
The PHP conditional assignment operators are used to set a value depending on conditions:
|?:||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||Returns the value of $x.|
The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.
Introduced in PHP 7