Advertisement
Google Ad Slot: content-top
PHP Operators
Operators are used to perform operations on variables and values in PHP.
Operators:
Arithmetic Operators:
Used to perform basic mathematical operations.
Operator |
Description |
Example |
Result |
|---|---|---|---|
|
+ |
Addition |
5 + 2 |
7 |
|
- |
Subtraction |
5 - 2 |
3 |
|
* |
Multiplication |
5 * 2 |
10 |
|
/ |
Division |
5 / 2 |
2.5 |
|
% |
Modulus (Remainder) |
5 % 2 |
1 |
|
** |
Exponentiation |
5 ** 2 |
25 |
|
++ |
Increment |
let a = 1; a++ |
2 |
|
-- |
Decrement |
let a = 1; a-- |
0 |
"; echo "Multiplication: " . ($a * $b) . "
"; echo "Division: " . ($a / $b) . "
"; echo "Remainder: " . ($a % $b) . "
"; echo "Exponentiation: " . ($a ** $b) . "
"; echo "Increment: " . ($a++) . "
"; echo "Decrement: " . ($a--) . "
"; ?>
Assignment Operators:
Used to assign values to variables.
Operator |
Description |
Example |
Equivalent To |
|---|---|---|---|
|
= |
Assign |
a = 5 |
----- |
|
+= |
Add and Assign |
a += 2 |
a = a + 2 |
|
-= |
Subtract and Assign |
a -= 2 |
a = a - 2 |
|
*= |
Multiply and Assign |
a *= 2 |
a = a * 2 |
|
/= |
Divide and Assign |
a /= 2 |
a = a / 2 |
|
%= |
Modulus and Assign |
a %= 2 |
a = a % 2 |
|
**= |
Exponentiate Assign |
|
a = a ** 2 |
"); $a -= 2; echo("Subtract and Assign: ".$a."
"); $a *= 2; echo("Multiply and Assign: ".$a."
"); $a /= 2; echo("Divide and Assign: ".$a."
"); $a %= 2; echo("Modulus and Assign: ".$a."
"); $a **= 2; echo("Exponentiate Assign: ".$a."
"); ?>
Comparison Operators:
Used to compare two values.
Operator |
Description |
Example |
Result |
|---|---|---|---|
|
== |
Equal to |
5 == "5" |
true |
|
=== |
Strict Equal (type & value) |
5 === "5" |
false |
|
!= |
Not Equal |
5 != "5" |
false |
|
!== |
Strict Not Equal |
5 !== "5" |
true |
|
> |
Greater Than |
5 > 2 |
true |
|
< |
Less Than |
5 < 2 |
false |
|
>= |
Greater or Equal |
5 >= 5 |
true |
|
<= |
Less or Equal |
5 <= 5 |
true |
"; echo((5 != "5") ? "Not Equal" : "Equal")."
"; echo((5 !== "5") ? "Strict Not Equal" : "Strict Equal")."
"; echo((5 > 2) ? "Greater Than" : "Less Than")."
"; echo((5 < 2) ? "Less Than" : "Greater Than")."
"; echo((5 >= 5) ? "Greater or Equal" : "Less or Equal")."
"; echo((5 <= 5) ? "Less or Equal" : "Greater or Equal")."
"; ?>
Logical Operators:
Used to combine or invert Boolean values.
Operator |
Description |
Example |
Result |
|---|---|---|---|
|
&& |
AND |
true && false |
false |
|
|| |
OR |
true || false |
true |
|
! |
NOT |
!true |
false |
Bitwise Operators:
Operate on binary numbers.
Operator |
Description |
Example |
Result |
|---|---|---|---|
|
& |
AND |
5 & 1 |
1 |
|
| |
OR |
5 | 1 |
5 |
|
^ |
XOR |
5 ^ 1 |
4 |
|
~ |
NOT |
~5 |
-6 |
|
<< |
Left Shift |
5 << 1 |
10 |
|
>> |
Right Shift |
5 >> 1 |
2 |
"); // XOR operator echo("XOR: ".(5 ^ 1)."
"); // NOT operator echo("NOT: ".(~5)."
"); // Left Shift operator echo("Left Shift: ".(5 << 1)."
"); // Right Shift operator echo("Right Shift: ".(5 >> 1)."
"); ?>
String Operators:
Used to concatenate strings.
Operator |
Description |
Example |
Result |
|---|---|---|---|
|
. |
Concatenation |
"Hello " . "World!" |
Hello World! |
|
.= |
Concatenation assignment |
$str = "Hello"; $str .= "World!" |
Hello World! |
Ternary Operator:
A shorthand for if-else.
Nullish Coalescing Operator (??)
Returns the right-hand value if the left-hand value is null or undefined.
Spaceship Operator (<=>)
Access nested object properties safely.
Compares two values and returns:
-1if left is smaller0if they are equal1if left is greater