PHP language provides us with a range of operators which allow us to perform diverse operations or actions on variables or values. These variables or values on which the operator performs actions are called operands.
For example, the addition symbol (+) is an operator that instructs PHP to add two variables or values, while the greater than symbol is an operator that instructs PHP to compare two values.
All PHP operators can be grouped into the following categories depending on the type of operation performed using them:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Increment/Decrement Operators
- Logical Operators
- String Concatenation Operators
- Array Operators
PHP Arithmetic Operators
Arithmetic operators can be used to perform common arithmetical operations, such as addition, subtraction, multiplication and so on with numerical variables and values. The following table shows the complete list of PHP's arithmetic operators:
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | $a + $b | Sum of $a and $b |
- | Subtraction | $a - $b | Difference of $a and $b |
* | Multiplication | $a * $b | Product of $a and $b |
/ | Division | $a / $b | Quotient of $a and $b |
% | Modulus | $a % $b | Remainder of $a divided by $b |
** | Exponentiation | $a**$b | Raising $a to the power of $b |
The following example shows us how arithmetic operators can be used while programming in PHP.
Example :
PHP Assignment Operators
Assignment operators are used with numerical values to assign values to variables. The assignment operator is “=”. When it is used with numerical values the operand on the left gets assigned the value of the expression on the right. However it has a different meaning when it is used with strings.
Operator | Name | Example | Result |
---|---|---|---|
= | Assign | $a = $b | $a = $b |
+= | Add and assign | $a += $b | $a = $a + $b |
-= | Subtract and assign | $a -= $b | $a = $a - $b |
*= | Multiply and assign | $a *= $b | $a = $a * $b |
/= | Divide and assign - Quotient | $a /= $b | $a = $a / $b |
%= | Divide and assign - Modulus | $a %= $b | $a = $a % $b |
The following example which shows assignment operators in action
Example:
PHP Comparison Operators
The Comparison Operators are used to compare any two values which could be a string or a number. Comparison operators are generally used to control the flow of the program.
Operator | Name | Example | Result |
---|---|---|---|
== | Equal | $a == $b | True - if $a is equal to $b |
=== | Identical | $a === $b | True only if $a is equal to $b and a & b are of the same type |
!= | Not equal | $a != $b | True - if $a is not equal to $b |
>< | Not equal | $a><$b | True - if $a is not equal to $b |
!== | Not identical | $a !== $b | True - if $a is not equal to $b, or they are not of the same type |
< | Less than | $a < $b | True if $a is less than $b |
> | Greater than | $a > $b | True if $a is greater than $b |
>= | Greater than or equal to | $a >= $b | True if $a is greater than or equal to $b |
<= | Less than or equal to | $a <= $b | True if $a is less than or equal to $b |
Comparison operators help us compare two values in a Boolean manner.
Example:
PHP Comparison Operators
The Comparison Operators are used to compare any two values which could be a string or a number.
Operator | Name | Effect |
---|---|---|
++$a | Pre-increment | Increments $a by one, then returns $a |
$a++ | Post-increment | Returns $a, then increments $a by one |
--$a | Pre-decrement | Decrements $a by one, then returns $a |
$a-- | Post-decrement | Returns $a, then decrements $a by one |
The following example shows the practical usage of the increment and decrement operators.
Example:
PHP Logical Operators
Logical operators are generally used to combine conditional statements.
Operator | Name | Example | Result |
---|---|---|---|
And | And | $a and $b | True if both $a and $b are true |
Or | Or | $a or $b | True if either $a or $b is true |
Xor | Xor | $a xor $b | True if either $a or $b is true, but not both |
&& | And | $a && $b | True if both $a and $b are true |
|| | Or | $a || $b | True if either $$a or $b is true |
! | Not | !$a | True if $a is not true |
The following example shows these logical operators in action:
Example:
Code:
PHP Array Operators
Array operators are operators which allow us to perform union and high level comparison operations on arrays.
Operator | Name | Example | Result |
---|---|---|---|
+ | Union | $a + $b | Union of $a and $b |
== | Equality | $a == $b | Appends $string2 to $string1 |
The following example shows you these string operators in action
Example:
PHP Array Operators
Array operators are operators which allow us to perform union and high level comparison operations on arrays.
Operator | Name | Example | Result |
---|---|---|---|
+ | Union | $a + $b | Union of $a and $b |
== | Equality | $a == $b | True if $a and $b have the same key/value pairs |
=== | Identity | $a === $b | True if $a and $b have the same key/value pairs in the same order and of the same types |
!= | Inequality | $a != $b | True if $a is not equal to $b |
Inequality | $a $b | True if $a is not equal to $b | |
!== | Non-identity | $a !== $b | True if $a is not identical to $b |
Below example shows these array operators in action
Example :