Operators
There are many operators used in PHP, so they are separated into few categories to make it easier to learn them all.
Arithmetic operators:
| Operator | Description | Example |
|---|---|---|
| + | Addition | 3+2 |
| - | Subtraction | 6-2 |
| * | Multiplication | 4*4 |
| / | Division | 18/3 |
| % | Modulus | 10%3 |
| ++ | Increment |
$x += 1 or $x = $x + 1 |
| -- | Decrement |
$x -= 1 or $x = $x - 1 |
Assignment operators:
Example:
$variable = 4;
$another_variable = $variable
Result:
$another_variable = 4
Combination Arithmetic & Assignment Operators:
|
Operator |
Description |
Example |
Equivalent Operation |
| += | Plus Equals | $x += 4 | $x = $x + 4 |
| -= | Minus Equals | $x -= 3 | $x = $x - 3 |
| *= | Multiply Equals | $x *= 7 | $x = $x * 7 |
| /= | Divide Equals | $x /= 2 | $x = $x / 2 |
| %= | Modulo Equals | $x %= 5 | $x = $x % 5 |
| .= | Concatenate Equals | $string.="hello" | $string = $string . "hello" |
Comparison Operators:
| Operator | English | Example | Result |
|---|---|---|---|
| == | is equal to | $x == $y | false |
| != | is not equal to | $x != $y | true |
| < | is less than | $x < $y | true |
| > | is greater than | $x > $y | false |
| <= | is less than or equal to | $x <= $y | true |
| >= | is greater than or equal to | $x >= $y | false |
Logical Operators:
| Operator | Description | Example | Result |
|---|---|---|---|
|
! |
not |
!$x x!=y; !(x==y) |
returns false, if $x is true returns true |
|
&& |
and |
$x && $y |
returns true, if both $x and $y are true, otherwise false |
|
|| |
or |
$x || $y |
returns true if either $x or $y or both are true, otherwise false |