Operators in PHP are essential tools for performing operations on variables and values. They make it possible to calculate, compare, assign, or manipulate data. These operators are divided into distinct groups to cater to specific functions.
Categories of PHP Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Increment/Decrement Operators
- Logical Operators
- String Operators
- Array Operators
- Conditional Assignment Operators
PHP Arithmetic Operators
Arithmetic operators allow you to perform mathematical calculations.
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | $yamaha + $honda | Sum of $yamaha and $honda |
- | Subtraction | $yamaha - $honda | Difference between $yamaha and $honda |
* | Multiplication | $yamaha * $honda | Product of $yamaha and $honda |
/ | Division | $yamaha / $honda | Quotient of $yamaha divided by $honda |
% | Modulus | $yamaha % $honda | Remainder of $yamaha divided by $honda |
** | Exponentiation | $yamaha ** $honda | $yamaha raised to the power of $honda |
PHP Assignment Operators
These operators assign values to variables. The =
operator is the simplest form of assignment.
Operator | Same as… | Description |
---|---|---|
= | $bmw = $audi | Assigns the value of $audi to $bmw |
+= | $bmw += $audi | Adds $audi to $bmw and assigns the result to $bmw |
-= | $bmw -= $audi | Subtracts $audi from $bmw and assigns the result to $bmw |
*= | $bmw *= $audi | Multiplies $bmw by $audi and assigns the result to $bmw |
/= | $bmw /= $audi | Divides $bmw by $audi and assigns the quotient to $bmw |
%= | $bmw %= $audi | Assigns the remainder of $bmw divided by $audi |
PHP Comparison Operators
Comparison operators compare values, either numeric or string.
Operator | Name | Example | Result |
---|---|---|---|
== | Equal | $nike == $puma | true if $nike equals $puma |
=== | Identical | $nike === $puma | true if $nike equals $puma and are of the same type |
!= | Not Equal | $nike != $puma | true if $nike is not equal to $puma |
<> | Not Equal | $nike <> $puma | true if $nike is not equal to $puma |
!== | Not Identical | $nike !== $puma | true if $nike is not equal to $puma or not the same type |
> | Greater Than | $nike > $puma | true if $nike is greater than $puma |
< | Less Than | $nike < $puma | true if $nike is less than $puma |
PHP Increment/Decrement Operators
Increment/Decrement operators adjust a variable’s value by one.
Operator | Description | Example |
---|---|---|
++$adidas | Pre-increment | Increment $adidas by 1, then return $adidas |
$adidas++ | Post-increment | Return $adidas , then increment it by 1 |
--$adidas | Pre-decrement | Decrement $adidas by 1, then return $adidas |
$adidas-- | Post-decrement | Return $adidas , then decrement it by 1 |
PHP Logical Operators
Logical operators combine multiple conditional statements.
Operator | Name | Example | Result |
---|---|---|---|
and | And | $toyota and $nissan | true if both $toyota and $nissan are true |
or | Or | $toyota or $nissan | true if at least one is true |
! | Not | !$toyota | true if $toyota is false |
PHP String Operators
String operators are used for manipulating strings.
Operator | Name | Example | Result |
---|---|---|---|
. | Concatenation | $brand1 . $brand2 | Combines $brand1 and $brand2 into a single string |
.= | Concatenation Assignment | $brand1 .= $brand2 | Appends $brand2 to $brand1 |
PHP Array Operators
Array operators allow comparisons between arrays or their combination.
Operator | Name | Example | Result |
---|---|---|---|
+ | Union | $toyota + $honda | Combines arrays $toyota and $honda , keeping unique keys |
== | Equality | $toyota == $honda | Returns true if $toyota and $honda have the same key-value pairs |
=== | Identity | $toyota === $honda | Returns true if $toyota and $honda are identical in type and order |
!= | Inequality | $toyota != $honda | Returns true if $toyota is not equal to $honda |
<> | Inequality | $toyota <> $honda | Returns true if $toyota is not equal to $honda |
!== | Non-identity | $toyota !== $honda | Returns true if $toyota and $honda are not identical in type or order |
PHP Conditional Assignment Operators
These operators allow assignment based on conditions.
Operator | Name | Example | Result |
---|---|---|---|
?: | Ternary | $bmw = ($audi > 10) ? $audi : $toyota | If $audi > 10 , assigns $audi to $bmw ; otherwise assigns $toyota |
?? | Null Coalescing | $bmw = $audi ?? $toyota | Assigns $audi to $bmw if $audi exists and is not NULL ; otherwise assigns $toyota |