## Operators

Operator! What city please?

### Let's glue the pieces together.

Okay, so we are not talking about the phone operator. Which I don't think exists anymore and I may have just dated myself. I think the occupation has gone the way of the factory lector, lamplighters, rat catchers (Europe), and bowling alley pinsetters. The term operator is used in several industries all with the connotation of a person, or device, that exerts direction on a process. Operators in PHP are not much different. With all they do the baseline principle is that they yield a result in the process with which they are used.

If 1 + 2 = 3 then the *operands* are 1 and 2 and the *operator*
is *" +"*.

- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
- Precedence

#### Arithmetic Operators

**(+) Addition** — The + operator adds two operands together. In
the example below $c will equal 30.

```
$a = 10;
$b = 20;
$c = $a + $b;
```

**(-) Subtraction** — The - operator subtracts the second operand
from the first. In the example below $c will equal 10.

```
$a = 20;
$b = 10;
$c = $a - $b;
```

**(*) Multiplication** — The - operator multiplies both operands.
In the example below $c will equal 200.

```
$a = 10;
$b = 20;
$c = $a * $b;
```

**(/) Division** — The / operator divides the operand that preceeds
the operator by the operand following the operator. This is the same as:
numerator (dividend) ÷ denominator (divisor).
In the example below $c will equal 4.

```
$a = 20;
$b = 5;
$c = $a / $b;
```

**(%) Modulus** — The % operator simply returns the remainder
following the division of the numerator by the denominator. In the
example below $c will equal 3.

```
$a = 7;
$b = 5;
$c = $a % $b;
```

**(++) Increment** — The ++ operator increases the integer value by
one to the operand. In the example below $a will equal 3.

```
$a = 2;
$a++;
```

**(--) Increment** — The -- operator decreases the integer value by
one to the operand. In the example below $a will equal 1.

```
$a = 2;
$a--;
```

#### Comparison Operators

**(==) Equal To** — The == checks if the value of the operand
to the left of the operator equals the one to the right.

```
(1 == 2) //This is false
(1 == 1) //This is true
```

**(===) Identical To** — The === checks if the value of
the operand to the left of the operator equals, and is the same type to,
the one to the right.

```
(1 == 2) //This is false
(1 == 1) //This is true
```

**(!=) (<>) Not Equal To** — The != checks if the value of the operand
to the left of the operator is not equal the one to the right.

```
(1 != 2) //This is true
(1 != 1) //This is false
(1 <> 2) //This is true
(1 <> 1) //This is false
```

**(!==) Not Identical To** — The !== checks if the value
of the operand to the left of the operator is not equal to, and the
same type as, the one to the right.

```
(1 !== 2) //This is true
(1 !== 1) //This is false
(true !== 1) //This is true
```

**(>) Greater Than** — The > checks if the value of
the operand to the left of the operator is greater than the one to the
right.

```
(1 > 1) //This is false
(2 > 1) //This is true
```

**(>=) Greater Than or Equal To** — The >= checks
if the value of the operand to the left of the operator is greater than
or equal to the one to the right.

```
(1 >= 2) //This is false
(1 >= 1) //This is true
(2 > 1) //This is true
```

**(<) Less Than** — The < checks if the value of
the operand to the left of the operator is greater than the one to the
right.

```
(1 < 1) //This is false
(1 < 2) //This is true
```

**(<=) Less Than or Equal To** — The <= checks if
the value of the operand to the left of the operator is greater than
or equal to the one to the right.

```
(2 < 1) //This is false
(1 < 1) //This is true
(1 < 2) //This is true
```

**(<=>) Spaceship** — The <=> operator returns
0 if both operands are equal, 1 if the left is greater, and -1 if the
right is greater. This is new to PHP as of version 7 and is not widely
used yet; however, this is a common operator found in Perl and Ruby
code bases.

```
(1 <=> 1) //Returns 0
(2 <=> 1) //Returns 1
(1 <=> 2) //Returns -1
```

**(??) Null Coalesce** — The ?? operator returns the first
operand, from left to right, that exists and is not NULL. This is another
new operator to PHP as of version 7.

```
(1 ?? 2 ?? 3) //Returns 1
(null ?? true ?? 1) //Returns true
(null ?? 'foobar') //Returns 'foobar'
```

#### Logical Operators

**(and)** — The *and* operator returns true if the
left and right operands are true.

```
(1 and true) //Returns true
('string' and 1) //Returns true
(1 and false) //Returns false
(0 and 1) //Returns false
```

**(or)** — The *or* operator returns true if the one
of the operands to the left or right are true.

```
(1 or true) //Returns true
(1 or 0) //Returns true
(1 or false) //Returns true
(0 or false) //Returns false
```

**(&&)** — The && operator returns true if the
left and right operands are true.

```
(1 && true) //Returns true
(1 && 'string') //Returns true
(1 && 0) //Returns false
(0 && false) //Returns false
```

**(||)** — The || operator returns true if the one
of the operands to the left or right are true.

```
(1 || true) //Returns true
(1 || 0) //Returns true
(1 || false) //Returns true
(0 || false) //Returns false
```

**(!)** — The ! operator is use to reverses the logical
state of its operand. If a condition is true then Logical NOT operator
will make false.

```
!(1 && true) //Returns false
!(1 && 'string') //Returns false
```

#### Assignment Operators

**(=) Simple Assignment** — The = assigns the value of the
operand on the right to the operand to the left.

`$variable_a = 2 + 3; //The value of $variable_a is set to 5`

**(+=) Add And Assignment** — The += adds the value of the
operand on the right to the operand to the left.

```
$variable_a = 2;
$variable_a += 3; //The value of $variable_a is set to 5
```

**(-=) Subtract And Assignment** — The -= subtracts the
value of the operand on the right from the operand to the left.

```
$variable_a = 10;
$variable_a -= 2; //The value of $variable_a is set to 8
```

**(*=) Multiply And Assignment** — The *= multiplies the
value of the operand on the right with the operand to the left.

```
$variable_a = 2;
$variable_a *= 2; //The value of $variable_a is set to 4
```

**(/=) Divide And Assignment** — The /= divides the
value of the operand on the left with the operand to the right.

```
$variable_a = 10;
$variable_a /= 2; //The value of $variable_a is set to 5 (same as 10 / 2)
```

**(%=) Modulus And Assignment** — The %= takes the modulus
of the two operands and assigns that value to the left operand.

```
$variable_a = 11;
$variable_a /= 2; //The value of $variable_a is set to 1 (same as 11 % 2)
```

#### Conditional Operator

**(? :) Conditional Expression** — The ? : expression is a
a basic if then conditional expression. The following reads that if
$variable_a is true then $variable_b is set to 'This is true'. Otherwise
it is set to 'This is false'.

```
$variable_a = true;
$variable_b = $variable_a ? 'This is true' : 'This is false'; //$variable_b is set to 'This is false'
```

#### Precedence

The precedence of the operators are listed from top to bottom.

Category | Operator | Precedence |
---|---|---|

Unary | ! ++ -- | Right to left |

Multiplicative | * / % | Left to right |

Additive | + - | Left to right |

Relational | < <= > >= | Left to right |

Equality | == != | Left to right |

Logical AND | && | Left to right |

Logical OR | || | Left to right |

Conditional | ?: | Right to left |

Assignment | = += -= *= /= %= | Right to left |