Operators in PHP are a symbol that is used to perform operations. In simple words, operators are used to executing the operations on variables or values

In PHP, there are too many operators are there and they are:

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

**Arithmetic Operators**

In PHP, arithmetic operators perform simple mathematical operations like addition, subtraction, multiplication, etc.

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 | It gives the result of raising $a to the $b’th power |

**Example**:

<?php $x = 4; // Variable 1 $y = 4; // Variable 2 // Some arithmetic operations on // these two variables echo ($x + $y), "\n"; echo($x - $y), "\n"; echo($x * $y), "\n"; echo($x / $y), "\n"; ?>

```
OUTPUT:
4
0
16
1
```

**Assignment Operators**

In PHP, the assignment operators assign values to different variables. “=” operator is used basically in assignment operator.

Operator | Name | Name | Result |

= | Assign | $ = $b | The value in the right operand is assigned to the left operand. |

+= | Add then Assign | $x += $b | Addition same as $x = $x + $b |

-= | Subtract then Assign | $x -= $b | Subtraction same as $x = $x – $b |

*= | Multiply then Assign | $x *= $b | Multiplication same as $x = $x * $b |

/= | Divide then Assign (quotient) | $x /= $b | Find quotient same as $x = $x / $b |

%= | Divide then Assign (remainder) | $x %= $b | Find remainder same as $x = $x % $b |

**Example:**

<?php // Simple assign operator $y = 75; echo $y, "\n"; // Add then assign operator $y = 100; $y += 200; echo $y, "\n"; // Subtract then assign operator $y = 70; $y -= 10; echo $y, "\n"; // Multiply then assign operator $y = 30; $y *= 20; echo $y, "\n"; // Divide then assign(quotient) operator $y = 100; $y /= 5; echo $y, "\n"; // Divide then assign(remainder) operator $y = 50; $y %= 5; echo $y; ?>

```
OUTPUT:
75
300
60
600
20
0
```

**Bitwise Operators**

In PHP, bitwise operators perform the bit-level operations on operands.

Operator | Name | Example | Explanation |

& | And | $x & $b | Bits that are 1 in both $x and $b are set to 1, otherwise 0. |

| | Or (Inclusive or) | $x | $b | Bits that are 1 in either $x or $b are set to 1 |

^ | Xor (Exclusive or) | $x ^ $b | Bits that are 1 in either $x or $b are set to 0. |

~ | Not | ~$x | Bits that are 1 are set to 0 and bits that are 0 are set to 1 |

<< | Shift left | $x << $b | Left shift the bits of operand $x $b steps |

>> | Shift right | $x >> $b | Right shift the bits of $x operand by $b number of places |

**Increment / Decrement Operators**

In PHP, the increment operators are used to increment a variable’s value.

In PHP, the decrement operators are used to decrement a variable’s value.

Operator | Name | Example | Explanation |

++ $x++ | Increment | ++$x | Increment the value of $x by one, then return $x Return $x, then increment the value of $x by one |

— $x– | decrement | –$x | Decrement the value of $x by one, then return $x Return $x, then decrement the value of $x by one |

**Example:**

<!DOCTYPE html> <html> <body> <?php $x = 34; echo ++$x; ?> </body> </html>

` OUTPUT: 35`

**Logical Operators**

In PHP, the logical operators perform bit-level operations on operands.

Operator | Name | Example | Explanation |

and | And | $x and $b | Return TRUE if both $x and $b are true |

Or | Or | $x or $b | Return TRUE if either $x or $b is true |

xor | Xor | $x xor $b | Return TRUE if either $ or $b is true but not both |

! | Not | ! $x | Return TRUE if $x is not true |

&& | And | $x && $b | Return TRUE if either $x and $b are true |

|| | Or | $x || $b | Return TRUE if either $x or $b is true |

**Example:**

<!DOCTYPE html> <html> <body> <?php $x = 100; $y = 50; if ($x == 100 or $y == 80) { echo "Hello oraltadalafil!"; } ?> </body> </html>

```
OUTPUT:
Hello oraltadalafil!
```

**String Operators**

In PHP, there are two operators that are specially designed for strings and used for the concatenation of 2 or more strings using the concatenation operator (‘.’).

Operator | Name | Example | Explanation |

. | Concatenation | $x . $y | Concatenate both $x and $y |

.= | Concatenation and Assignment | $a .= $b | First concatenate $a and $b, then assign the concatenated string to $a, e.g. $a = $a . $b |

**Example:**

<!DOCTYPE html> <html> <body> <?php $txt1 = "Hello"; $txt2 = " oraltadalafi!"; echo $txt1 . $txt2; ?> </body> </html>

` OUTPUT: Hello oraltadalafil!`

**Array Operators**

The Array operators are used in the case of arrays. The array operators and their syntax and operations, that PHP provides for the array operation. To compare arrays we need the array operators.

Operator | Name | Example | Explanation |

+ | Union | $a + $y | Union of $a and $b |

== | Equality | $a == $y | Return TRUE if $a and $y have the same key/value pair |

!= | Inequality | $a != $y | Return TRUE if $a is not equal to $y |

=== | Identity | $a === $y | Return TRUE if $a and $y have the same key/value pair of the same type in the same order |

!== | Non-Identity | $a !== $y | Return TRUE if $a is not identical to $y |

<> | Inequality | $a <> $y | Return TRUE if $a is not equal to $y |

**Conditional Assignment Operators**

In PHP, the conditional assignment operators are used to fix a value depending on conditions:

Operator | Name | Example | Result |

?: | Ternary | $x = expr1 ? expr2 : expr3 | Returns the value of $a. The value of $a is expr2 if expr1 = TRUE. The value of $a is expr3 if expr1 = FALSE |

?? | Null coalescing | $x = expr1 ?? expr2 | Returns the value of $a. The value of $a is expr1 if expr1 exists, and is not NULL. If expr1 does not exist or is NULL, the value of $a is expr2. Introduced in PHP 7 |

**Example:**

<!DOCTYPE html> <html> <body> <?php // if empty($user) = TRUE, set $status = "oraltadalafil" echo $status = (empty($user)) ? "GeektoCode" : "logged in"; echo("<br>"); $user = "User"; // if empty($user) = FALSE, set $status = "logged in" echo $status = (empty($user)) ? "oraltadalafil" : "logged in"; ?> </body> </html>

```
OUTPUT:
oraltadalafil
logged in
```

## 1 thought on “What are Operators in PHP?”