In PHP, operators are symbols or combinations of symbols used to perform operations on variables and values. Here's an overview of the main types of operators in PHP:
1. Arithmetic Operators
Used to perform common arithmetic operations:
Operator |
Name |
Example |
+ |
Addition |
$a + $b |
- |
Subtraction |
$a - $b |
* |
Multiplication |
$a * $b |
/ |
Division |
$a / $b |
% |
Modulus |
$a % $b |
** |
Exponentiation |
$a ** $b |
2. Assignment Operators
Used to assign values to variables:
Operator |
Example |
Equivalent |
= |
$a = $b |
Assign |
+= |
$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 |
3. Comparison Operators
Used to compare values:
Operator |
Name |
Example |
== |
Equal |
$a == $b |
=== |
Identical (type + value) |
$a === $b |
!= or <> |
Not equal |
$a != $b |
!== |
Not identical |
$a !== $b |
> |
Greater than |
$a > $b |
< |
Less than |
$a < $b |
>= |
Greater or equal |
$a >= $b |
<= |
Less or equal |
$a <= $b |
<=> |
Spaceship (returns -1, 0, or 1) |
$a <=> $b |
4. Increment / Decrement Operators
Operator |
Name |
Example |
++$a |
Pre-increment |
Increments before use |
$a++ |
Post-increment |
Increments after use |
--$a |
Pre-decrement |
Decrements before use |
$a-- |
Post-decrement |
Decrements after use |
5. Logical Operators
Operator |
Name |
Example |
and |
And |
$a and $b |
or |
Or |
$a or $b |
xor |
Xor |
$a xor $b |
&& |
And (stronger precedence) |
$a && $b |
` |
|
` |
! |
Not |
!$a |
6. String Operators
Operator |
Name |
Example |
. |
Concatenation |
$a . $b |
.= |
Concatenate and assign |
$a .= $b |
7. Array Operators
Operator |
Name |
Example |
+ |
Union |
$a + $b |
== |
Equality |
$a == $b |
=== |
Identity |
$a === $b |
!= |
Inequality |
$a != $b |
!== |
Non-identity |
$a !== $b |
8. Bitwise Operators
Operator |
Name |
Example |
& |
And |
$a & $b |
` |
` |
Or |
^ |
Xor |
$a ^ $b |
~ |
Not |
~$a |
<< |
Shift left |
$a << 1 |
>> |
Shift right |
$a >> 1 |