# PHP Operators

You will be using PHP Operators to perform actions on certain values. You can use them to perform various actions within your PHP. Such as math, comparisons, for conditions and for assignments to name a few.

Make sure to give a good read at this last part of the PHP Basics tutorials as there are plenty of operators to remember. You can always come back here though for a quick review if needed!

### PHP Operators TOC

- Assignment.
- Arithmetic.
- Logical.
- Comparison.
- Incrementing & Decrementing.
- String.
- Array.
- Spaceship.
- Null Coalescing.
- Ternary.

### Assignment

`=`

, $a gets set to the value of $b (equals) `$a = $b`

`+=`

, $a gets set to $a + $b (addition) `$a += $b`

`-=`

, $a gets set to $a - $b (subtraction) `$a -= $b`

`*=`

, $a gets set to $a * $b (multiplication) `$a *= $b`

`/=`

, $a gets set to $a / $b (division) `$a /= $b`

`%=`

, $a gets set to $a % $b (modulus) `$a %= $b`

`**=`

, $a gets set to $a ** $b (exponentiation) `$a **= $b`

### Arithmetic

`+`

, sum of $a and $b `$a + $b`

`-`

, difference of $a and $b `$a - $b`

`*`

, product of $a and $b `$a * $b`

`/`

, quotient of $a and $b `$a / $b`

`%`

, remainder of $a / $b `$a % $b`

`**`

, result of raising $a to the $b‘th power `$a ** $b`

### Logical

`and`

, true if both $a and $b are true `$a and $b`

`&&`

, true if both $a and $b are true `$a && $b`

`or`

, true if either $a or $b is true `$a or $b`

`||`

, true if either $a or $b is true `$a || $b`

`xor`

, true if either $a or $b is true, but not both `$a xor $b`

`!`

, true if $a is not true `!$a`

### Comparison

`==`

, true if values of $a and $b are equal `$a == $b`

`===`

, true if both values and data types of $a and $b are equal `$a === $b`

`!=`

, true if $a is not equal to $b `$a != $b`

`<>`

, true if $a is not equal to $b `$a <> $b`

`!==`

, true if $a is not equal to $b or they are not in the same data type `$a !== $b`

`>`

, true if $a is greater than $b `$a > $b`

`<`

, true if $a is less than $b `$a < $b`

`>=`

, true if $a is greater than or equal to $b `$a >= $b`

`<=`

, true if $a is less than or equal to $b `$a <= $b`

### Incrementing & Decrementing

`++`

, increments $a by one, then returns $a `++$a`

`++`

, returns $a, then increments $a by one `$a++`

`--`

, decrements $a by one, then returns $a `--$a`

`--`

, returns $a, then decrements $a by one `$a--`

### String

`.`

, returns the concatenation of its $a and $b `$a . $b`

`.=`

, $a gets set to $a . $b `$a .= $b`

### Array

`+`

, returns the union of $a and $b arrays `$a + $b`

`+=`

, $a gets set to $a + $b `$a += $b`

`==`

, true if $a and $b has equal key/value pairs `$a == $b`

`===`

, true if $a and $b has equal key/value pairs in the same order and of same data type `$a === $b`

`!=`

, true if $a and $b are not equal `$a != $b`

`<>`

, true if $a and $b are not equal `$a <> $b`

`!==`

, true if $a and $b has different orders, different key/value pairs or values of different data types `$a !== $b`

### Spaceship

`<=>`

, returns 0 since both sides are equal `1 <=> 1`

`<=>`

, returns 1 as left side is greater `2 <=> 1`

`<=>`

, returns -1 as right side is greater `1 <=> 2`

The spaceship operator was added since PHP 7. You can compare integers, floats or strings.

### Null Coalescing

`??`

, returns 1 if $a was null `$a ?? 1`

The null coalescing operator was introduced since PHP 7. It's basically like an `if`

conditional but it only does one thing.

If the variable checked is null, it will assign it the new value.

```
<?php
/**
* If the $name is null
* Echo 'Konstantinos'
*/
$name = null;
echo $name ?? 'Konstantinos';
/**
* A better example with multiple nulls.
*
* This would output 5.
*
* $a is null so $b is assigned.
* $b is null so $c is assigned.
* $c has a value so that is echoed.
* $d is not importatnt as we already have a value.
*
*/
echo '<br>';
$a = null;
$b = null;
$c = 5;
$d = 10;
echo $a ?? $b ?? $c ?? $d;
```

### Ternary

`?:`

, returns 1 if $a was null `$a ?: 1`

The ternary operator is like a short `if`

conditional as well.

Its full syntax is like this to understand it a bit better.

`(condition) ? ( do_something ) : ( do_something )`

Since PHP 5.3, you can omit the true expression making it even easier to use.

Note that even though you can nest multiple ternary operators similar to the conditional if statements I wouldn’t recommend it as it would be really hard to read.

```
<?php
/**
* If the number is 1
* echo "The number is 1."
* else
* echo "There is no number!"
*/
$number = rand( 0, 1 );
echo ( 1 === $number ) ? 'The number is 1.' : 'There is no number!';
/**
* If you only like to echo $number on true
* this could be the shorter version.
*/
echo '<br>Shorter version: ';
echo ( 1 === $number ) ?: 'There is no number!';
```

### Extra Tips for PHP Operators

Make sure to be comparing equal data types. Comparing or doing math etc on strings vs numbers, for example, will be returning wrong results, if not errors.