# PHP Operators

An operator is a symbol that takes one or more values (operands) and yields another value.

For example: 4 + 5 = 9. Here symbol “+” is an operator which takes 4 and 5 as values(operands) for operation addition and result 9 as output.

### PHP language supports operators in the following groups:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators

## 1. PHP Arithmetic Operators

The arithmetic operators are used to perform common arithmetic operations such as addition, subtraction, multiplication etc.

Operator | Name | Example | Result |

+ | Addition | $x + $y | Sum of $x & $y |

– | Substraction | $x – $y | Difference of $x & $y |

* | Multiplication | $x * $y | Product of $x & $y |

/ | Division | $x / $y | Quotient of $x & $y |

% | Modulus | $x % $y | Remainder of $x divided by $y |

<?php $x = 14; $y = 4; //Addition echo($x + $y); // 0utputs: 18 //Substraction echo($x - $y); // 0utputs: 10 //Multiplication echo($x * $y); // 0utputs: 56 //Division echo($x / $y); // 0utputs: 3 //Modulus echo($x % $y); // 0utputs: 2 ?>

## 2. PHP Assignment Operators

The Assignment operators are used to assign values to different variable.

Operators | Description | Example |

= | Assigns values from right side to left side operand | x=y |

+= | Add & Assignment operator. It adds right operand to the left operand & assigns result to the left operand. | x +=y |

-= | Substract & Assignment operator. It substract right operand to the left operand & assigns result to the left operand. | x -=y |

*= | Multiply & assignment operator. It multiplies the right operand to the left operand & assigns result to the left operand. | x *=y |

/= | Divide & assignment operator. It divides left operand with the right operand & assigns the result to the left operand. | x /=y |

%= | Modulus & assignment operator. It makes modulus using two operands & assigns the value to the left operands. | x %=y |

## 3. PHP Comparison Operators

The PHP Comparison Operators are used to compare two values.

Operators | Name | Example | Description |

== | Equal | $x==$y | Returns true if $x is equal to $y |

=== | Identical | $x===$y | Returns true if $x is equal and same type of $y. |

!= | Not equal | $x != $y | Returns true if $x is not equal to $y. |

<> | Not equal | $x <> $y | Returns true if $x is not equal to $y. |

!== | Not identical | $x !== $y | Returns true if $x is not equal or are not the same types to $y |

> | Greater than | $x>$y | Returns true if the $x is greater than $y |

< | Less than | $x<$y | Returns true if the $x is less than $y |

>= | Greater than or equal to | $x >= $y | Returns true if the $x is greater than or equal to $y |

<= | Less than or equal to | $x <= $y | Returns true if the $x is less than or equal to $y |

## 4. PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable’s value. And PHP decrement operators are used to decrements a variable’s value.

Operators | Name | Description |

++$x | Pre-increment | Increments $x by one, and returns $x. |

$x++ | Post-increment | Returns $x first, then increment $x by one. |

– -$x | Pre-decrement | Decrements $x by one and returns $x. |

$x- – | Post-decrement | Returns $x first then decrements $x by one. |

## 5. PHP Logical Operators

Logical operators are very important as it comes use in real world program. The PHP logical operators are used to combine conditional statements.

Operator | Name | Example | Result |

&& | And | $x && $y | Returns true if both $x & $y are true. |

|| | Or | $x || $y | Returns true if any of $x, $y is true. |

and | And | $x and $y | Returns true if both $x & $y are true. |

or | Or | $x or $y | Returns true if any of $x, $y is true. |

xor | Xor | $x Xor $y | Returns true if either $x or $y is true, but not both. |

! | Not | !$x<dsf | Returns true if $x is not true. |

## 6. PHP String Operators

There are two string operators. The first is String Operator(‘.’) and the second one is concatenating assignment operator (‘.=’).

**.**Concatenation**.=**Combined Concatenation

Let’s us understand with example –

**Concatenation Example (‘.’) –**

<?php $firstname = "Share"; $lastname = "Query"; echo $firstname . " ". $lastname ; ?> Output: Share Query

**Combined Concatenation Example (‘.=’) – **

<?php $mystring = 'Welcome to '; $mystring .= 'Share '; $mystring .= 'Query'; echo $mystring; ?> Output: Welcome to Share Query

## 7. PHP Array Operators

The PHP array operators are used to compare with arrays.

Operator | Name | Example | Result |

+ | Union | $x + $y | Union of $x and $y |

== | Equality | $x == $y | Returns true if $x & $y have the same key /value pairs. |

=== | Identical | $x ===$y | Returns true if $x & $y have the same key/value pairs in the same order & of same types. |

!= | Inequality | $x != $y | Returns true if array $x is not equal to array $y. |

<> | Inequality | $x <> $y | Returns true if array $x is not equal to $y. |

!== | Non-identical | $x !== $y | Returns true if array $x is not identical to array $y. |

## 8. PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions/expressions.

There are two conditional assignment operators in PHP –

**Ternary (‘?:’) –****Null Coalescing (‘??’) –**

**Ternary Operator – **Ternary operator actually means “an operator which involves on three operands.

The Ternary Operator will result lefthand operand when the condition evaluates to true. And the righthand operand is used for default or false values.

`$result = $condition ? $condition : 'default';`

Example –

<?php //if empty($user) = TRUE $status = (empty($user)) ? "Please Login First" : "Logged In"; echo $status; //Output: Please Login First //if empty($user) = FALSE: $user = "Kumud"; $status = (empty($user)) ? "Please Login First" : "Logged In"; echo $status; //Output: Logged In ?>

**Null Coalescing – **Null Coalescing operator (??) is used to replace the ternary operator with the help of isset() function. The Null coalescing operator returns its first operand if it exists & it is not NULL otherwise it will return the second operand.

Example –

<?php //If user is not logged in $status = $_GET['username'] ?? "Please Login" ; echo $status; //Chaining Operation using Null Coalescing $status = $_GET['username'] ?? $_POST['username'] ?? 'Please Login'; echo $status; ?>