## PHP Tutorial

# PHP Operators

PHP Operators can be used to perform operations on one or more values.

## PHP Assignment Operators

The `=`

basic operator can be used to assign the value on the right to the variable on the left.

Operator |
Operation |

`$a = $b` |
Variable Assignment |

`$a += $b` |
Addition |

`$a -= $b` |
Subtraction |

`$a /= $b` |
Division |

`$a *= $b` |
Multiplication |

`$a **= $b` |
Exponentiation |

`$a %= $b` |
Modulus |

## PHP Arithmetic Operators

The PHP arithmetic operators can be used to perform arithmetic operations.

Operator |
Operation |

`$a + $b` |
Addition |

`$a - $b` |
Subtraction |

`$a / $b` |
Division |

`$a * $b` |
Multiplication |

`$a ** $b` |
Exponentiation |

`$a % $b` |
Modulus |

## PHP Comparison Operators

The PHP Comparison operators can be used to make comparisons between two values or data types.

Operator |
Operation |

`$a == $b` |
Checks if `$a` and `$b` are equal. |

`$a === $b` |
Checks if `$a` and `$b` are of the same datatype. |

`$a != $b` |
Checks if `$a` is not equal to `$b` . |

`$a > $b` |
Checks if `$a` is greater than `$b` . |

`$a < $b` |
Checks if `$a` is less than `$b` . |

`$a <> $b` |
Checks if `$a` is not equal to `$b` . |

`$a >= $b` |
Checks if `$a` is greater than or equal to `$b` . |

`$a <= $b` |
Checks if `$a` is less than or equal to `$b` . |

`$a <=> $b` |
Also known as the spaceship operator, Returns `-1` if `$a` is less than `$b` , returns `0` is `$a` is equal to `$b` and returns `1` if `$a` is greater than `$b` . Introduced in PHP 7. |

a

## PHP Logical Operators

PHP Logical Operators can be used to perform operations between two variables or values.

Operator |
Operation |

`$a && $b` |
True if both `$a` and `$b` are `true` . |

`$a || $b` |
True if either `$a` or `$b` is `true` . |

`!$a` |
True if `$a` is `false` , also known as Logical NOT. |

`$a and $b` |
True if both `$a` and `$b` are `true` . |

`$a or $b` |
True if either `$a` or `$b` is `true` . |

`$a xor $b` |
True if either `$a` or `$b` is `true` but not both. |

## PHP Conditional Assignment Operators

Operator |
Operation |

`$a ? echo "Hello" : echo "Hi";` |
The Ternary Operator outputs Hello if `$a` is `true` or outputs Hi if `$a` is `false` . |

`$a ?? $b` |
Null Coalescing Operator returns the value of `$b` if `$a` is NULL or the value of `$a` if it is not `NULL` |

## PHP Array Operators

PHP Array Operators can be used to perform operations on arrays.

Operator |
Operation |

`$a + $b ` |
Union of `$a` and `$b` . |

`$a <> $b` |
Returns true if `$a` is not equal to `$b` . |

`$a != $b` |
Return true if `$a` is not equal to `$b` . |

`$a !== $b` |
Returns true if `$a` is not of the same key/value pairs as `$b` , if `$a` and `$b` are not of the same type. |

`$a == $b` |
Returns true if `$a` is equal to `$b` . |

`$a === $b` |
Returns true if `$a` is of the same key/value pairs as `$b` , if `$a` and `$b` are of the same type. |

## PHP String Operators

PHP String Operators can be used to perform operations on strings.

Operator |
Operation |

`$a . $b ` |
Returns the concatenation of `$a` and `$b` . |

`$a .= $b` |
Appends the value of `$b` to `$a` . |

## PHP Increment/Decrement Operators

PHP Increment Operators can be used to perform operations on strings.

Operator |
Operation |

`$a++` |
Returns `$a` before incrementing it. |

`++$a` |
Increments `$a` before returning it. |

`$a--` |
Returns `$a` before decrementing it. |

`--$a` |
Decrements `$a` before returning it. |

## What You Should Know at The End of This Lesson

- You should know about the different types of PHP Operators.
- You should know how to perform operations with each type of operator.
- you should know the difference between
`++$var`

and`$var++`

.