# PHP Operators

Examples:

`+`is an arithmetic**operator**which represents**addition**.`-`is an arithmetic**operator**which represents**subtraction**.

PHP has 11 types of operators.

- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Incrementing/Decrementing Operators
- String Operators
- Array Operators
- Type Operators
- Bitwise Operators
- Error Control Operators
- Execution Operators

Also, we have a new useful operator added in PHP 7.

**Note:** In this tutorial, we will only learn about the first 7 types of operators, as they are the ones that used most commonly.

## PHP Arithmetic Operators

This is almost the same as the basic arithmetic operators you learned in your school.

Operator | Name | Example | Result | |
---|---|---|---|---|

+ | Addition | $a + $b | Sum of $a and $b | Run Example ›› |

- | Subtraction | $a - $b | Difference of $a and $b | Run Example ›› |

* | Multiplication | $a * $b | Product of $a and $b | Run Example ›› |

/ | Division | $a / $b | Quotient of $a and $b | Run Example ›› |

% | Modulus | $a % $b | Remainder of $a / $b | Run Example ›› |

** | Exponentiation | $a ** $b | Result of raising $a to the $b'th power | Run Example ›› |

## PHP Assignments Operators

The basic assignment operator in PHP is `=`. It is actually not the "equal" mark in PHP. It works as assignment operator, which assigns a value to a variable.

### PHP Basic Assignment Operator Example

```
<?php
$a = 5;
$b = 7;
$a = $b;
echo $a; // outputs 7
```

Run Example ››In this example,

- 5 is assigned to
`$a`. - 7 is assigned to
`$b`. - Then,
**the value of**`$b`(7) is assigned to`$a`.

There are some other assignment operators to learn.

Operator | Example | Long Form | Description | |
---|---|---|---|---|

= | $a = $b | $a = $b | $a gets set to the value of $b | Run Example ›› |

+= | $a += $b | $a = $a + $b | $a gets set to $a + $b (Addition) | Run Example ›› |

-= | $a -= $b | $a = $a - $b | $a gets set to $a - $b (Subtraction) | Run Example ›› |

*= | $a *= $b | $a = $a * $b | $a gets set to $a * $b (Multiplication) | Run Example ›› |

/= | $a /= $b | $a = $a / $b | $a gets set to $a / $b (Division) | Run Example ›› |

%= | $a %= $b | $a = $a % $b | $a gets set to $a % $b (Modulus) | Run Example ›› |

**= | $a **= $b | $a = $a ** $b | $a gets set to $a ** $b (Exponentiation) | Run Example ›› |

## PHP Comparison Operators

Comparison Operators allow you to compare two values.

Operator | Name | Example | Result | |
---|---|---|---|---|

== | Equal | $a == $b | True if values of $a and $b are equal | Run Example ›› |

=== | Identical | $a === $b | True if both values and data types of $a and $b are equal | Run Example ›› |

!= | Not equal | $a != $b | True if $a is not equal to $b | Run Example ›› |

<> | Not equal | $a <> $b | True if $a is not equal to $b | Run Example ›› |

!== | Not identical | $a !== $b | True if $a is not equal to $b or they are not in the same data type | Run Example ›› |

> | Greater than | $a > $b | True if $a is greater than $b | Run Example ›› |

< | Less than | $a < $b | True if $a is less than $b | Run Example ›› |

>= | Greater than or equal to | $a >= $b | True if $a is greater than or equal to $b | Run Example ›› |

<= | Less than or equal to | $a <= $b | True if $a is less than or equal to $b | Run Example ›› |

`!==`and

`<>`have

**no difference**. Both work in the same way.

## PHP Logical Operators

A logical operator is a kind used in logic. In PHP, all logical operators return a boolean value.

Operator | Name | Example | Result | |
---|---|---|---|---|

and | and | $a and $b | True if both $a and $b are true | Run Example ›› |

&& | and | $a && $b | True if both $a and $b are true | Run Example ›› |

or | or | $a or $b | True if either $a or $b is true | Run Example ›› |

|| | or | $a || $b | True if either $a or $b is true | Run Example ›› |

xor | xor | $a xor $b | True if either $a or $b is true, but not both | Run Example ›› |

! | not | !$a | True if $a is not true | Run Example ›› |

Note: We will discuss more about the `if` blocks we have used in this examples in the next lesson.

## PHP Incrementing/Decrementing Operators

**Note:**

- Only strings and numbers (integers and floats) are affected by these operators.
- Arrays and objects are not affected.
- Decrementing null has no effect, but incrementing results in 1.

Example | Name | Result | |
---|---|---|---|

++$a | Pre-increment | Increments $a by one, then returns $a | Run Example ›› |

$a++ | Post-increment | Returns $a, then increments $a by one. | Run Example ›› |

--$a | Pre-decrement | Decrements $a by one, then returns $a | Run Example ›› |

$a-- | Post-decrement | Returns $a, then decrements $a by one. | Run Example ›› |

## PHP String Operators

There are two special string operators what help us to operate strings.

Operator | Name | Example | Result | |
---|---|---|---|---|

. | Concatenation | $a . $b | Returns the concatenation of its right and left arguments | Run Example ›› |

.= | Concatenation Assignment | $a .= $b | $a gets set to $a . $b | Run Example ›› |

## PHP Array Operators

PHP array operators are used to perform operations on arrays.

Operator | Name | Example | Result | |
---|---|---|---|---|

+ | Union | $a + $b | Returns the union of $a and $b arrays. | Run Example ›› |

+= | Union Assignment | $a += $b | $a gets set to $a + $b | Run Example ›› |

== | Equal | $a == $b | True if $a and $b has equal key/value pairs | Run Example ›› |

=== | Identical | $a === $b | True if $a and $b has equal key/value pairs in the same order and of same data type. | Run Example ›› |

!= | Not equal | $a != $b | True if $a and $b are not equal. | Run Example ›› |

<> | Not equal | $a <> $b | True if $a and $b are not equal. | Run Example ›› |

!== | Not identical | $a !== $b | True if $a and $b has different orders, different key/value pairs or values of different data types. | Run Example ›› |

## Spaceship Operator

Spaceship Operator (`<=>`) is a combined comparison operator which was added in PHP 7. Integers, floats, and strings can be compared.

When | Return Value |
---|---|

Either side are equal | 0 |

The left side is greater | 1 |

The right side is greater | -1 |

### PHP Spaceship Operator - Integer Comparison

```
<?php
var_dump(1 <=> 1); // outputs 0
var_dump(1 <=> 2); // outputs -1
var_dump(2 <=> 1); // outputs 1
```

Run Example ››### PHP Spaceship Operator - Float Comparison

```
<?php
var_dump(1.5 <=> 1.5); // outputs 0
var_dump(1.5 <=> 2.5); // outputs -1
var_dump(2.5 <=> 1.5); // outputs 1
```

Run Example ››### PHP Spaceship Operator - String Comparison

```
<?php
var_dump('a' <=> 'a'); // outputs 0
var_dump('a' <=> 'b'); // outputs -1
var_dump('b' <=> 'a'); // outputs 1
var_dump('eat' <=> 'ear'); // outputs 1
```

Run Example ››### PHP Spaceship Operator - Mixed Comparison

```
<?php
var_dump(1 <=> 2.5); // outputs -1
var_dump(2.5 <=> 1); // outputs 1
var_dump('1' <=> 1); // outputs 0
var_dump(1 <=> '2'); // outputs -1
var_dump('2' <=> 1); // outputs 1
```

Run Example ››**"dog"**with an integer like

**10**can return unexpected results.