PHP Operators – Types, Examples & Usage Guide
Operators are symbols or keywords in PHP that perform actions on values, like calculations, comparisons, or logic tasks.
1. Arithmetic Operators
1. Arithmetic Operators
Used for mathematical calculations.
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | $a + $b |
Sum of $a and $b |
- |
Subtraction | $a - $b |
Difference of $a and $b |
* |
Multiplication | $a * $b |
Product of $a and $b |
/ |
Division | $a / $b |
Quotient of $a and $b |
% |
Modulus (remainder) | $a % $b |
Remainder of $a divided by $b |
** |
Exponentiation | $a ** $b |
$a raised to the power of $b |
Example:
<?php $a = 21; $b = 4; echo $a + $b; // Outputs: 25 echo $a % $b; // Outputs: 1 ?>
2. Assignment Operators
Used to assign values to variables.
| Operator | Example | Equivalent |
|---|---|---|
= |
$a = $b |
$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 |
Example:
<?php $a = 5; $a += 10; // Equivalent to $a = $a + 10 echo $a; // Outputs: 15 ?>
3. Comparison Operators
Used to compare two values. They return a boolean value (true or false).
| Operator | Description | Example | Result |
|---|---|---|---|
== |
Equal | $a == $b |
true if $a is equal to $b |
=== |
Identical (equal and same type) | $a === $b |
true if $a equals $b and both are of the same type |
!= |
Not equal | $a != $b |
true if $a is not equal to $b |
<> |
Not equal | $a <> $b |
Same as != |
!== |
Not identical | $a !== $b |
true if $a is not equal or not the same type as $b |
> |
Greater than | $a > $b |
true if $a is greater than $b |
< |
Less than | $a < $b |
true if $a is less than $b |
>= |
Greater than or equal to | $a >= $b |
true if $a is greater than or equal to $b |
<= |
Less than or equal to | $a <= $b |
true if $a is less than or equal to $b |
<=> |
Spaceship (PHP 7+) | $a <=> $b |
Returns -1, 0, or 1 |
Example:
<?php $a = 5; $b = 10; echo $a < $b; // Outputs: 1 (true) echo $a === "5"; // Outputs: (false, different types) ?>
4. Logical Operators
Used for logical operations.
| Operator | Description | Example | Result |
|---|---|---|---|
&& |
And | $a && $b |
true if both $a and $b are true |
| ` | ` | Or | |
! |
Not | !$a |
true if $a is false |
and |
And (lower priority) | $a and $b |
Same as && |
or |
Or (lower priority) | $a or $b |
Same as ` |
Example:
<?php $a = true; $b = false; echo $a && $b; // Outputs: (false) echo $a || $b; // Outputs: 1 (true) ?>
5. Increment/Decrement Operators
Used to increase or decrease the value of a variable.
| Operator | Description | Example |
|---|---|---|
++$a |
Pre-increment | Increases $a by 1, then returns $a |
$a++ |
Post-increment | Returns $a, then increases $a by 1 |
--$a |
Pre-decrement | Decreases $a by 1, then returns $a |
$a-- |
Post-decrement | Returns $a, then decreases $a by 1 |
Example:
<?php $a = 6; echo ++$a; // Outputs: 7 (pre-increment) echo $a--; // Outputs: 7 (post-decrement) echo $a; // Outputs: 6 ?>
6. String Operators
Used to manipulate strings.
| Operator | Description | Example | Result |
|---|---|---|---|
. |
Concatenation | $a . $b |
Combines $a and $b |
.= |
Concatenation assignment | $a .= $b |
Appends $b to $a |
Example:
<?php $a = "Hello"; $b = " World"; echo $a . $b; // Outputs: Hello World $a .= $b; echo $a; // Outputs: Hello World ?>
7. Array Operators
Used to compare or manipulate arrays.
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Union | $a + $b |
Combines $a and $b |
== |
Equality | $a == $b |
true if $a and $b have the same key-value pairs |
=== |
Identity | $a === $b |
true if $a and $b are identical |
!= |
Inequality | $a != $b |
true if $a and $b are not equal |
<> |
Inequality | $a <> $b |
Same as != |
!== |
Non-identity | $a !== $b |
true if $a and $b are not identical |
Example:
<?php
$a = array("x" => 1, "y" => 2);
$b = array("z" => 3, "x" => 1);
$c = $a + $b; // Union
print_r($c);
// Outputs: Array ( [x] => 1 [y] => 2 [z] => 3 )
?>