# PHP - Operators

Operators are used to perform operation on two operands. Operators in PHP can be categorized as follows:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• Conditional (or Ternary) operators

## Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

OperatorDescription
- Subtraction
* Multiplication
/ Division
** Exponent / Powera**b is equivalent to a^b (ex: 10**2 is equal to 100)
% Modulusa%b returns remainder of a/b (ex: 10 % 3 returns 1)

## Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

Operator Expression Equivalent to
= a = 5 a = 5
+= a += b a = a + b
-= a -= b a = a - b
*= a *= b a = a * b
/= a /= b a = a / b
**= a **= b a = a ** b
%= a %= b a = a % b

## Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and false when values doen not match.

Operator Description
== Equal: Checks the values of two operands and returns true if they are same.
=== Identical: Checks the values and datatypes of two operands and returns true if they are same.
!= Not equal: Checks the values of two operands and returns true if they are not same.
<> Not equal
!== Not identical: Checks the values and datatypes of two operands and returns true if they are not same.
> Greater than: Checks the values of two operands and returns true if the value of first operand is greater than the value of second operand.
< Less than: Checks the values of two operands and returns true if the value of first operand is less than the value of second operand.
>= Greater than or equal to: Checks the values of two operands and returns true if the value of first operand is greater than or equal to the value of second operand.
<= Less than or equal to: Checks the values of two operands and returns true if the value of first operand is less than or equal to the value of second operand.
<=> Spaceship: Checks the values of two operands and returns values based on the values of two operands:
• Returns -1 if the value of first operand is less than value of second operand.
• Returns 0 if the values of two operands are equal.
• Returns 1 if the value of first operand is greater than value of second operand.

## Increment/Decrement operators

Increment and decrement operators are used to increase and decrease the value of variable.

Operator Description
++$x Pre-increment: Increases the value of$x by 1, the returns $x.$x++ Post-increment: Returns $x, then increases the value of$x by 1.
--$x Pre-decrement: Decreases the value of$x by 1, the returns $x.$x-- Post-decrement: Returns $x, then decreases the value of$x by 1.

## Logical operators

Logical operators are used to combine two or more conditions.

Operator NameDescription
and And Returns true when all conditions are true.
Example: ($x and$y) returns true when $x and$y are true.
or Or Returns true when any of the conditions is true.
Example: ($x or$y) returns true when either $x or$y is true.
xor Xor Returns true when any of the conditions is true, but not both.
Example: ($x Xor$y) returns true when either $x or$y is true but not both.
&& And Returns true when all conditions are true.
Example: ($x and$y) returns true when $x and$y are true.
|| Or Returns true when any of the conditions is true.
Example: ($x or$y) returns true when either $x or$y is true.
! Not Returns true when the given conditions is not true.
Example: (!$x) returns true when$x is not true.

## Conditional (or Ternary) operators

The conditional (or ternary) operators are used to set a value depending on conditions.

Operator NameExample
?: Ternary $x = exp1 ? exp2 : exp3 • The value of$x is exp2 if exp1 is TRUE.
• The value of $x is exp3 if exp1 is FALSE. ?? Null coalescing$x = exp1 ?? exp2
• The value of $x is exp1 if exp1 exists and not NULL. • The value of$x is exp2 if exp1 does not exist, or is NULL.