# How to use PHP operators

PHP operators provide simple and efficient methods for **data manipulation**. Due to their compact form, the code can often be made more understandable and intuitive.

## What are PHP operators?

PHP operators are **special symbols or strings** for operations on [PHP variables](https://www.ionos.com/digitalguide/websites/web-development/php-variable/) and values. They’re used to manipulate data, check conditions, and perform mathematical operations. They are often included in [PHP functions](https://www.ionos.com/digitalguide/websites/web-development/php-functions/) or [PHP classes](https://www.ionos.com/digitalguide/websites/web-development/php-classes/). But operators also perform essential tasks in complex applications. For example, you can [use PHP to retrieve information from a MySQL database](https://www.ionos.com/digitalguide/websites/web-development/use-php-to-retrieve-information-from-a-mysql-mariadb-database/) and compare or link data using operators.

Tip With [Deploy Now](https://www.ionos.com/hosting/deploy-now "Deploy Now from IONOS") from IONOS you can easily build and deploy sites. Auto-scaling capabilities ensure your applications always deliver the performance they need, without manual intervention. This saves time and resources, so you can focus on developing your projects.

## What types of PHP operators are there?

PHP operators perform many operations, including **calculations, value assignments, comparisons, and logical expressions**.

Here are some of the main PHP operator types:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment and decrement operators
- Logical operators
- String operators
- Array operators
- Conditional operators

### Arithmetic operators

Arithmetic operators are used to perform mathematical calculations.

<table>
  <thead>
    <tr>
      <th>Operator</th>
      <th>Name</th>
      <th>Example</th>
      <th>Result</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`+`</td>
      <td>Addition</td>
      <td>`$x + $y`</td>
      <td>Sum of $x and $y</td>
    </tr>
    <tr>
      <td>`-`</td>
      <td>Subtraction</td>
      <td>`$x - $y`</td>
      <td>Difference between $x and $y</td>
    </tr>
    <tr>
      <td>`*`</td>
      <td>Multiplication</td>
      <td>`$x*  $y`</td>
      <td>Product from $x to $y</td>
    </tr>
    <tr>
      <td>`/`</td>
      <td>Division</td>
      <td>`$x / $y`</td>
      <td>Quotient of $x and $y</td>
    </tr>
    <tr>
      <td>`%`</td>
      <td>Modulus</td>
      <td>`$x % $y`</td>
      <td>Rest of $x divided by $y</td>
    </tr>
    <tr>
      <td>`**`</td>
      <td>Exponentiation</td>
      <td>`$x**  $y`</td>
      <td>Result of raising $x to the $y’th power</td>
    </tr>
  </tbody>
</table>

### Assignment operators

Assignment operators like `=, +=, -=, *=, /=` are used to assign values to variables and update variable values.

<table>
  <thead>
    <tr>
      <th>Assignment</th>
      <th>Equivalent to</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`x = y`</td>
      <td>`x = y`</td>
      <td>The value of y is assigned to x</td>
    </tr>
    <tr>
      <td>`x += y`</td>
      <td>`x = x + y`</td>
      <td>Addition</td>
    </tr>
    <tr>
      <td>`x -= y`</td>
      <td>`x = x - y`</td>
      <td>Subtraction</td>
    </tr>
    <tr>
      <td>`x *= y`</td>
      <td>`x = x*  y`</td>
      <td>Multiplication</td>
    </tr>
    <tr>
      <td>`x /= y`</td>
      <td>`x = x / y`</td>
      <td>Division</td>
    </tr>
    <tr>
      <td>`x %= y`</td>
      <td>`x = x % y`</td>
      <td>Modulus</td>
    </tr>
  </tbody>
</table>

### Comparison operators

These operators compare values and evaluate conditions.

<table>
  <thead>
    <tr>
      <th>Operator</th>
      <th>Name</th>
      <th>Example</th>
      <th>Result</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`==`</td>
      <td>Equal</td>
      <td>`$x == $y`</td>
      <td>True, when $x equals $y</td>
    </tr>
    <tr>
      <td>`===`</td>
      <td>Identical</td>
      <td>`$x === $y`</td>
      <td>True, when $x and $y are identical and the same type</td>
    </tr>
    <tr>
      <td>`!=`</td>
      <td>Not equal</td>
      <td>`$x != $y`</td>
      <td>True, when $x does not equal $y</td>
    </tr>
    <tr>
      <td>`<>`</td>
      <td>Not equal</td>
      <td>`$x <> $y`</td>
      <td>True, when $x does not equal $y</td>
    </tr>
    <tr>
      <td>`!==`</td>
      <td>Not identical</td>
      <td>`$x !== $y`</td>
      <td>True, when $x does not equal $y and are not the same type</td>
    </tr>
    <tr>
      <td>`>`</td>
      <td>Greater than</td>
      <td>`$x > $y`</td>
      <td>True, when $x is larger than $y</td>
    </tr>
    <tr>
      <td>`<`</td>
      <td>Less than</td>
      <td>`$x < $y`</td>
      <td>True, when $x is smaller than $y</td>
    </tr>
    <tr>
      <td>`>=`</td>
      <td>Less than or equal to</td>
      <td>`$x >= $y`</td>
      <td>True, when $x is larger or equal to $y</td>
    </tr>
    <tr>
      <td>`<=`</td>
      <td>Greater than or equal to</td>
      <td>`$x <= $y`</td>
      <td>True, when $x is smaller or equal to $y</td>
    </tr>
    <tr>
      <td>`<=>`</td>
      <td>Spaceship</td>
      <td>`$x <=> $y`</td>
      <td>Integer, which is smaller, equal to, or larger than 0, when $x is smaller than, equal to, or larger than $y</td>
    </tr>
  </tbody>
</table>

### Increment and decrement operators

Incrementing or decrementing increases or decreases the value of variables.

<table>
  <thead>
    <tr>
      <th>Operator</th>
      <th>Name</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`++$x`</td>
      <td>Pre-increment</td>
      <td>Increases $x by one and returns $x</td>
    </tr>
    <tr>
      <td>`$x++`</td>
      <td>Post-increment</td>
      <td>Returns $x and then increases $x by one</td>
    </tr>
    <tr>
      <td>`--$x`</td>
      <td>Pre-decrement</td>
      <td>Decreases $x by one and returns $x</td>
    </tr>
    <tr>
      <td>`$x--`</td>
      <td>Post-decrement</td>
      <td>Returns $x, then decreases $x by one</td>
    </tr>
  </tbody>
</table>

### Logical operators

Logical operators are used in PHP to create logical expressions and to link or negate conditions.

<table>
  <thead>
    <tr>
      <th>Operator</th>
      <th>Name</th>
      <th>Example</th>
      <th>Result</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`and`</td>
      <td>And</td>
      <td>`$x and $y`</td>
      <td>True, when $x and $y are true</td>
    </tr>
    <tr>
      <td>`or`</td>
      <td>Or</td>
      <td>`$x or $y`</td>
      <td>True, when $x or $y are true</td>
    </tr>
    <tr>
      <td>`xor`</td>
      <td>Xor</td>
      <td>`$x xor $y`</td>
      <td>True, if either $x or $y are true, but not both</td>
    </tr>
    <tr>
      <td>`&&`</td>
      <td>And</td>
      <td>`$x && $y`</td>
      <td>True, when $x and $y are true</td>
    </tr>
    <tr>
      <td>`</td><td></td><td>`</td>
      <td>Or</td>
    </tr>
    <tr>
      <td>`!`</td>
      <td>Not</td>
      <td>`!$x`</td>
      <td>True, when $x is not true</td>
    </tr>
  </tbody>
</table>

### String operators

These operators work with strings.

<table>
  <thead>
    <tr>
      <th>Operator</th>
      <th>Name</th>
      <th>Example</th>
      <th>Result</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`.`</td>
      <td>Concatenation</td>
      <td>`$txt1 . $txt2`</td>
      <td>Connects $txt1 and $txt2</td>
    </tr>
    <tr>
      <td>`.=`</td>
      <td>Linking assignment operator</td>
      <td>`$txt1 .= $txt2`</td>
      <td>Links $txt2 to $txt1</td>
    </tr>
  </tbody>
</table>

### Array operators

There are special PHP operators to link arrays in PHP or to add values in arrays.

<table>
  <thead>
    <tr>
      <th>Operator</th>
      <th>Name</th>
      <th>Example</th>
      <th>Result</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`+`</td>
      <td>Union</td>
      <td>`$x + $y`</td>
      <td>Unifies $x and $y</td>
    </tr>
    <tr>
      <td>`==`</td>
      <td>Equality</td>
      <td>`$x == $y`</td>
      <td>True, when $x und $y Have the same key/value pairs</td>
    </tr>
    <tr>
      <td>`===`</td>
      <td>Identity</td>
      <td>`$x === $y`</td>
      <td>True, when $x and $y have the same key/value pairs, are in the same order, and are the same type</td>
    </tr>
    <tr>
      <td>`!=`</td>
      <td>Inequality</td>
      <td>`$x != $y`</td>
      <td>True, when $x does not equal $y</td>
    </tr>
    <tr>
      <td>`<>`</td>
      <td>Inequality</td>
      <td>`$x <> $y`</td>
      <td>True, when $x does not equal $y</td>
    </tr>
    <tr>
      <td>`!==`</td>
      <td>Non-identity</td>
      <td>`$x !== $y`</td>
      <td>True, when $x and $y are not identical</td>
    </tr>
  </tbody>
</table>

### Conditional operators

These operators assign values based on different conditions.

<table>
  <thead>
    <tr>
      <th>Operator</th>
      <th>Name</th>
      <th>Example</th>
      <th>Result</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>`?:`</td>
      <td>Ternary operator</td>
      <td>`$x = expr1 ? expr2 : expr3`</td>
      <td>Returns the value of $x; If expr1 is true, $x is equal to expr2, otherwise $x is equal to expr3.</td>
    </tr>
    <tr>
      <td>`??`</td>
      <td>Zero coherant operator</td>
      <td>`$x = expr1 ?? expr2`</td>
      <td>Returns the value of $x; The value of $x is expr1, when expr1 is available and is not NULL, otherwise $x is equal to expr2.</td>
    </tr>
  </tbody>
</table>

Tip You’ll find essential PHP programming concepts in our [PHP tutorial](https://www.ionos.com/digitalguide/websites/website-creation/learn-php-our-all-encompassing-php-tutorial-for-beginners/). We also recommend checking out our articles on [PHP vs. Python](https://www.ionos.com/digitalguide/websites/web-development/php-vs-python/) and [PHP vs. JavaScript](https://www.ionos.com/digitalguide/websites/web-development/php-vs-javascript/) for an even better understanding of PHP.

## Example of PHP operators in practice

PHP operators can be used in many ways and in different parts of your code.

### Operators in control structures

In the following example, we’ll implement **concatenation and comparison operators** for conditional statements.

```php
$age = 25;
$legalAge = 18;
if ($age > $legalAge) {
echo "Your age is: " . $age . " You are old enough to vote.";
} else {
echo "Your age is: " . $age . " You are not old enough to vote.";
}
```

Here, we’re comparing the age (`$age`) with the legal age (`$legalAge`) for the right to vote. The **comparison operator (`>`) checks** if `$age` is greater than `$legalAge`. Depending on whether the condition is true or not, we’ll see whether the person is old enough to choose and connect the age to the concatenation operator. Since `$age` is greater than `$legalAge`, the condition is true.


This is a markdown version of: [https://www.ionos.com/digitalguide/websites/web-development/php-operators/](https://www.ionos.com/digitalguide/websites/web-development/php-operators/) for AI/LLM consumption.