**Table of Contents**

- Introduction
- Arithmetic Operators
- Arithmetic Operator Precedence
- Assignment Operators
- Increment/Decrement Operators
- Comparison Operators
- The Ternary Operator
- Logical Operators

## Introduction

In mathematics, operators let us specify the mathematical operations to perform on numbers, such as addition, subtraction, multiplication, and division. A mathematical expression is a combination of values, variables, operators, and functions. For example,

$$4(x^2 + 2x - 6).$$

The corresponding **PHP expression** would be

`4*($x**2 + 2*$x - 6)`

.

In PHP programming, in addition to the arithmetic operators, several other types of operators exist too.

- Arithmetic Operators
- Assignment operators
- Increment/Decrement Operators
- Comparison Operators
- Logical Operators

The above operators can be classified as one of the following 3 types depending on the number of operands required.

**Unary operators**: take a single operand (e.g. increment (`$x++`

), logical not (`!$x`

).**Binary operators**: take two operands (e.g. addition (`$x+$y`

), subtraction (`$x-$y`

).**Ternary operator**: requires three operands (e.g.`expr ? x : y`

). It’s a single-line`if`

statement that returns`x`

if`expr`

is`TRUE`

and`y`

if`expr`

is`FALSE`

.

## Arithmetic Operators

The PHP arithmetic operators are used to perform common arithmetic operations on variables. In addition to the four main operations (add, subtract, multiply, and divide), we can also perform exponentiation `**`

and compute the modulo (`%`

). The following table summarizes the arithmetic operations.

Operator | Type | Description | Example |
---|---|---|---|

`+` |
Binary | Addition | `$x+$y` |

`-` |
Binary | Subtraction | `$x-$y` |

`*` |
Binary | Multiplication | `$x*$y` |

`/` |
Binary | Division | `$x/$y` |

`%` |
Binary | Modulo | `$x%$y` |

`**` |
Binary | Exponentiation | `$x**2` |

Example

Addition and subtraction operators.```
1<?php
2 $x = 20;
3 $y = 10;
4 echo $x+$y; // addition
5 echo "<BR>";
6 echo $x-$y; // subtraction
7?>
```

```
30
10
```

Example

Multiplication and division operators.```
1<?php
2 $a = 2;
3 $b = 3;
4 echo $a*$b; // multiplication
5 echo "<BR>";
6 echo $b/$a; // division
7?>
```

```
6
1.5
```

Example

Modulo and exponentiation operators.```
1<?php
2 $a = 10;
3 $b = 3;
4 echo $a%$b; // modulo
5 echo "<BR>";
6 echo $b**3; // exponentiation
7?>
```

```
1
27
```

ADVERTISEMENT

## Arithmetic Operator Precedence

Similar to Python, PHP arithmetic operators also obey the **PEMDAS** rule to determine the precedence of multiple operators appearing in an expression. In short, powers are executed before multiplication and division, which are in turn executed before addition and subtraction.

First Letter | Order of Precedence | Operator |
---|---|---|

P | Parentheses |
`()` |

E | Exponentiation |
`**` |

M | Multiplication |
`*` |

D | Division |
`/` |

A | Addition |
`+` |

S | Subtraction |
`-` |

The precedence of operators above is listed from High to low. For M and D, we perform (M)ultiplication or (D)ivision from *left to right* based on whichever operation comes first. Similarly, for A and S, we perform (A)ddition or (S)ubtraction from left to right based on whichever operation comes first.

Example

Arithmetic operator precedence.```
1<?php
2 $a = 6;
3 $b = 3;
4 $c = 2;
5 echo $a+$b*$c; // multiplication > addition
6 echo "<BR>";
7 echo $b*$c**3; // exponentiation > multiplication
8 echo "<BR>";
9 echo $a-$b+$c; // addition & subtraction -> left to right
10 echo "<BR>";
11 echo $a/$b*$c; // multiplication & division -> left to right
12?>
```

```
12
24
5
4
```

We can often use parentheses to prioritize operations within the parentheses.

Example

Arithmetic operator precedence using parentheses.```
1<?php
2 $a = 6;
3 $b = 3;
4 $c = 2;
5 echo ($a+$b)*$c; // 18
6 echo "<BR>";
7 echo ($b*$c)**3; // 216
8 echo "<BR>";
9 echo $a-($b+$c); // 1
10 echo "<BR>";
11 echo $a/($b*$c); // 1
12 echo "<BR>";
13 echo $a/(12/$b*$c); // 0.75
14?>
```

```
18
216
1
1
0.75
```

In the last example, `$a/(12/$b*$c)`

evaluates to `6/(12/3*2)`

$\rightarrow$ `6/(4*2)`

$\rightarrow$ `6/(8)`

$\rightarrow$ `0.75`

. The operators within the parentheses also follow the PEDMAS rule.

## Assignment Operators

In addition to the assignment operator `=`

, PHP has several other similar operators that make our life easier. Consider for example the assignment statement `$x = $x + 2`

. This can be simplied as `$x += 2`

where `+=`

is the addition assignment operator that adds the value on the right side to the variable on the left. Other similar assignment operators are summarized below.

Operator | Type | Description | Example | Meaning |
---|---|---|---|---|

`=` |
Binary | Assignment | `$x = 2` |
`$x = 2` |

`+=` |
Binary | Addition | `$x += 5` |
`$x = $x + 5` |

`-=` |
Binary | Subtraction | `$x -= 2` |
`$x = $x - 2` |

`*=` |
Binary | Multiplication | `$x *= 3` |
`$x = $x * 3` |

`/=` |
Binary | Division | `$x /= 4 ` |
`$x = $x / 4` |

`.=` |
Binary | Concatenation | `$x .= $y` |
`$x = $x . $y` |

`%=` |
Binary | Modulo | `$x %= 3` |
`$x = $x % 3` |

The `.`

is the concatenation operator as discussed in the topic on strings. The operator can also be applied to numeric variables.

Example

Using assignment operators.```
1<?php
2 $a = 6; $b = 3; $c = 2; $d = 4; $e = 8;
3 echo $a += 2; // 8
4 echo "<BR>";
5 echo $b -= 2; // 1
6 echo "<BR>";
7 echo $c *= 2; // 4
8 echo "<BR>";
9 echo $d /= 2; // 2
10 echo "<BR>";
11 echo $e %= 3; // 2
12?>
```

```
8
1
4
2
2
```

Example

Using the concatenation operator.```
1<?php
2 $a = 6; $b = 3; $c = 2;
3 echo $a .= $b; // 63
4 echo "<BR>";
5 echo $c .= $a; // 263
6?>
```

```
63
263
```

ADVERTISEMENT

## Increment/Decrement Operators

The PHP **increment** operators are used to increment a variable’s value whereas the PHP **decrement** operators are used to decrement a variable’s value. The following table summarizes the increment/decrement operations.

Operator | Type | Description | Example |
---|---|---|---|

`++` |
Unary | Pre-increment | `++$x` |

`++` |
Unary | Post-increment | `$x++` |

`--` |
Unary | Pre-decrement | `--$y` |

`--` |
Unary | Post-decrement | `$y--` |

Example

4 ways of making a**unit increment**to a variable.

```
1<?php
2 $count = 1;
3 $count = $count + 1; // $count=2
4 echo $count . "<br>";
5 $count += 1; // $count=3
6 echo $count . "<br>";
7 $count++; // $count=4
8 echo $count . "<br>";
9 ++$count; // $count=5
10 echo $count . "<br>";
11?>
```

```
2
3
4
5
```

Example

4 ways of making a**unit decrement**to a variable.

```
1<?php
2 $count = 1;
3 $count = $count - 1; // $count=0
4 echo $count . "<br>";
5 $count -= 1; // $count=-1
6 echo $count . "<br>";
7 $count--; // $count=-2
8 echo $count . "<br>";
9 --$count; // $count=-3
10 echo $count . "<br>";
11?>
```

```
0
-1
-2
-3
```

Example

Making an**increment**to a variable.

```
1<?php
2 $count = 0;
3 $count = $count + 10;
4 echo $count . "<br>";
5 $count += 10;
6 echo $count . "<br>";
7?>
```

```
10
20
```

Example

Making a**decrement**to a variable.

```
1<?php
2 $count = 20;
3 $count = $count - 5 ;
4 echo $count . "<br>";
5 $count -= 8;
6 echo $count . "<br>";
7?>
```

```
15
7
```

In the following example, the pre-increment operator on `$x`

tells PHP to first increment the value of `$x`

and *then* to test whether its value is `11`

and, if it does, to output its value.

Example

Making a**pre-increment**.

```
1<?php
2 $x = 10;
3 if (++$x == 11) echo $x; //pre-increment
4?>
```

```
11
```

In the following example, the post-increment operator on `$x`

tells PHP to first test whether its value is `10`

and if it does, to output its value after incrementing its value.

Example

Making a**post-increment**.

```
1<?php
2 $x = 10;
3 if ($x++ == 10) echo $x; //post-increment
4?>
```

```
11
```

## Comparison Operators

Comparison operators are generally used inside a construct such as an `if`

statement
in which you need to compare two items. For example, you may wish to know whether a variable you have been incrementing has reached a specified value.

Operator | Type | Description | Example |
---|---|---|---|

`==` |
Binary | is equal to | `$x == $y` |

`!=` |
Binary | is not equal to | `$x != $y` |

`>` |
Binary | is greater than | `$x > $y` |

`<` |
Binary | is less than | `$x < $y` |

`>=` |
Binary | is greater than or equal to | `$x >= $y` |

`<=` |
Binary | is less than or equal to | `$x <= $y` |

`===` |
Binary | is identical to | `$x === $y` |

`!==` |
Binary | is not identical | `$x !== $y` |

Example

Comparison operators.```
1<?php
2 $a = 5; $b = 6; $c = 7;
3 echo "Is $a equal to $b: " , $a == $b;
4 echo "<BR>";
5 echo "Is $a not equal to $b: " , $a != $b;
6 echo "<BR>";
7 echo "Is $c greater than to $b: " , $c > $b;
8 echo "<BR>";
9 echo "Is $c less than to $b: " , $c < $b;
10?>
```

```
Is 5 equal to 6:
Is 5 not equal to 6: 1
Is 7 greater than to 6: 1
Is 7 less than to 6:
```

**Review**

Recall that PHP will return nothing (`null`

) if the expression evaluates to `false`

and return a `1`

if it evaluates to `true`

.

The operator `===`

is also called an **identity** operator. `$x === $y`

will only return `true`

if both variables are equal in **value** and **type**. On the other hand, `$x == $y`

will return `true`

as long as the values are equal, regardless of the data type.

Example

Using the identity operator.```
1<?php
2 $a = 10;
3 $b = "10";
4 $c = 10;
5 if ($a == $b) echo "a and b are equal." . "<BR>";
6 if ($a === $b) echo "a and b are identical" . "<BR>";
7 if ($a === $c) echo "a and c are identical" . "<BR>";
8?>
```

```
a and b are equal.
a and c are identical
```

In the above example, `$a`

and `$b`

have the same value `10`

, therefore, `$a == $b`

returns `1`

and the accompanying statement is printed. However, `$a`

and `$b`

are of different types, therefore `$a === $b`

returns `null`

and the statement is not printed. Finally `$a === $c`

is `true`

since both `$a`

and `$c`

share the same value and data type.

## The Ternary Operator

The ternary operator `?:`

is similar to the `if... else`

statement and used in the form `(condition) ? (statement1) : (statement2)`

which will return either `statement1`

or `statement2`

depending on whether `condition`

evaluates to `TRUE`

or `FALSE`

.

Operator | Type | Description | Example |
---|---|---|---|

`expr ? x : y` |
Ternary | Returns `x` if `expr` is `TRUE` and `y` if `expr` is `FALSE` . |
`$x==$y ? "equal" : "not equal"` |

In the following example, the string `"pass"`

is printed since the condition `$score>=50`

evaluates to `true`

.

Example

Using the ternary operator.```
1<?php
2 $score=62;
3 echo ($score>=50) ? "pass" : "Fail";
4?>
```

```
pass
```

Example

Assigning result of the ternary operator to a variable.```
1<?php
2 $score=62;
3 $result = ($score>=50) ? "pass" : "Fail";
4 echo $result
5?>
```

```
pass
```

ADVERTISEMENT

## Logical Operators

Comparison operators are generally used inside a construct such as an `if`

statement
in which you need to compare two items. For example, you may wish to know whether a variable you have been incrementing has reached a specified value.

Operator | Type | Description | Example |
---|---|---|---|

`&&` |
Binary | AND | `$i == 1 && $j == 2` |

`and` |
Binary | low-precedence AND | `$i == 1 and $j == 2` |

`|| ` |
Binary | OR | `$k < 3 || $k > 10` |

`or` |
Binary | low-precedence OR | `$k < 3 or $k > 10` |

`!` |
Unary | NOT | `! ($i == $j )` |

`xor` |
Binary | exclusive OR | `$i xor $j ` |

The `&&`

and `and`

operators return the same results. The only difference between the `&&`

and `and`

operators are their precedences. The same can be said of the difference between the `||`

and `or`

operators.

The following table shows all the possible combinations of the logical operators applied to 2 operands `a`

and `b`

and their outcomes. For example, if `a`

is `false`

and `b`

is `true`

, then `a and b`

will return `false`

, `a or b`

will return `true`

and `a xor b`

will return `true`

.

Inputs | Results | |||
---|---|---|---|---|

a | b | AND | OR | XOR |

`TRUE` |
`TRUE` |
`TRUE` |
`TRUE` |
`FALSE` |

`TRUE` |
`FALSE` |
`FALSE` |
`TRUE` |
`TRUE` |

`FALSE` |
`TRUE` |
`FALSE` |
`TRUE` |
`TRUE` |

`FALSE` |
`FALSE` |
`FALSE` |
`FALSE` |
`FALSE` |

The `xor`

operator applied to `a xor b`

will produce `true`

if **only one** of the two operands is `true`

.

Example

Using the logical operators.```
1<?php
2 $a = 1; $b = 0;
3 echo "1:" . ($a && $b) . "<br>";
4 echo "2:" . ($a || $b) . "<br>";
5 echo "3:" . ($a xor $b) . "<br>";
6 echo "4:" . !$a . "<br>";
7?>
```

```
1:
2:1
3:1
4:
```

Only the second and third expressions return a `true`

.