PHP has following types of operators:
- Assignment Operators
- Comparison Operators
- Increment/Decrement Operators
- Logical Operators
- Arithmetic Operators
PHP Assignment Operators
The basic assignment operator is “=” in PHP. It means that the left operand gets set to the value of the expression on the right. That is, the value of “$var = 3” is 3.
Assignment | Same Operator | Description |
$x = $y | $x = $y | Left operand gets set to the value of the expression on the right |
$x += $y | $x = $x + $y | Addition |
$x -= $y | $x = $x – $y | Subtraction |
$x *= $y | $x = $x * $y | Multiplication |
$x /= $y | $x = $x / $y | Division |
$x %= $y | $x = $x % $y | Modulus |
$a .= $b | $a = $a.$b | Concatenate two strings |
PHP Increment/Decrement Operators
Operator | Name | Description |
++$x | Pre-increment | Increments $x by one then returns $x |
$x++ | Post-increment | Returns $x, then increments $x by one |
–$x | Pre-decrement | Decrements $x by one then returns $x |
$x– | Post-decrement | Returns $x, then decrements $x by one |
PHP Arithmetic Operators
Operator | Name | Description |
$x + $y | Addition | Sum of $x and $y |
$x – $y | Subtraction | Difference of $x and $y |
$x * $y | Multiplication | Product of $x and $y |
$x / $y | Division | Quotient of $x and $y |
$x % $y | Modulus | Reminder of $x divided by $y |
-$x | Negation | Opposite of $x |
$a.$b | Concatenation | Concatenate two strings |
Comparison Operators in PHP
Comparison operators allows you to compare two values in PHP programming.
Operator | Name | Description | Example |
$x == $y | Equal | True if $x is equal to $y | 3==5 returns false |
$x === $y | Identical | True if $x is equal to $y, and they are of the same type | 5===”5″ returns false |
$x != $y | Not equal | True if $x is not equal to $y | 3!=5 returns true |
$x <> $y | Not equal | True if $x is not equal to $y | 3<>5 returns true |
$x !== $y | Not Identical | True if $x is not equal to $y, or they are not of same type | 8!==”8″ returns true |
$x > $y | Greater than | True if $x is greater than $y | 5>3 returns true |
$x < $y | Less than | True if $x is less than $y | 3<5 returns true |
$x >= $y | Greater than or equal to | True if $x is greater than or equal to $y | 3>=5 returns false |
$x <= $y | Less than or equal to | True if $x less than or equal to $y | 3<=5 returns true |
Logical Operators in PHP
Operator | Name | Description | Example |
$x and $y | And | True if both $x and $y are true | $x=5 $y=3 ($x <10 and $y > 1) returns true |
$x or $y | Or | True if either or both $x and $y are true | $x=5 $y=3 ($x==5 or $y==6) returns true |
$x xor $y | Xor | True if either $x or $y is true, but not both | $x=5 $y=3 ($x==5 xor $y==3) returns false |
$x && $y | And | True if both $x and $y are true | $x=5 $y=3 ($x< 8 && $y >1) returns true |
$x || $y | Or | True if either or both $x and $y are true | $x=3 $y=5 ($x==4 || $y==4) returns false |
!$x | Not | True if $x is not true | $x=3 $y=6 !($x==$y) returns true |
PHP is a popular open-source general-purpose scripting language. You can learn PHP programming with examples by following our PHP articles sequentially.
350 total views, 1 views today