Operators in PHP are symbols used to perform operations on variables and values. They are essential for calculations, comparisons, decision-making, and logical processing in PHP programs.
Used for mathematical operations.
| Operator | Description | Example |
|---|---|---|
+ | Addition | $a + $b |
- | Subtraction | $a - $b |
* | Multiplication | $a * $b |
/ | Division | $a / $b |
% | Modulus (remainder) | $a % $b |
** | Exponentiation | $a ** $b |
Example:
Used to assign values to variables.
| Operator | Description |
|---|---|
= | Assign |
+= | Add and assign |
-= | Subtract and assign |
*= | Multiply and assign |
/= | Divide and assign |
%= | Modulus and assign |
Example:
Used to compare two values.
| Operator | Description |
|---|---|
== | Equal |
=== | Identical (value + type) |
!= | Not equal |
!== | Not identical |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
<=> | Spaceship |
Example:
Used for combining conditions.
| Operator | Description |
|---|---|
&& | AND |
| ` | |
! | NOT |
and | AND |
or | OR |
Example:
| Operator | Description |
|---|---|
++$a | Pre-increment |
$a++ | Post-increment |
--$a | Pre-decrement |
$a-- | Post-decrement |
Example:
| Operator | Description |
|---|---|
. | Concatenation |
.= | Concatenate and assign |
Example:
| Operator | Description |
|---|---|
+ | Union |
== | Equal |
=== | Identical |
!= | Not equal |
Example:
Used to handle null values.
| Operator | Description |
|---|---|
instanceof | Checks object type |
Example:
Operators in PHP are fundamental building blocks for performing calculations, making decisions, and processing data. A solid understanding of operators helps you write efficient and logical PHP code, especially when moving toward advanced topics and frameworks like Laravel.
Take quizzes related to this topic and see where you stand!
Start Quiz Now