# How to use PHP operators

Operators are used to perform operations on variables and values. PHP has many operators, including arithmetic, assignment, comparison, and logical operators.

## Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations. The following table shows the arithmetic operators in PHP:

Operator Description Example
```\$x = 10;
\$y = 20;
\$z = \$x + \$y;
// \$z = 30```
Subtraction
```\$x = 10;
\$y = 20;
\$z = \$y - \$x;
// \$z = 10```
* Multiplication
```\$x = 5;
\$y = 6;
\$z = \$x * \$y;
// \$z = 30```
/ Division
```\$x = 12;
\$y = 6;
\$z = \$x / \$y;
// \$z = 2```
% Modulus
```\$x = 10;
\$y = 3;
\$z = \$x % \$y;
// \$z = 1```
** Exponentiation
```\$x = 2;
\$y = 3;
\$z = \$x ** \$y;
// \$z = 8```

## Assignment Operators

Assignment operators are used to assign values to variables. The following table shows the assignment operators in PHP:

Operator Description Example
= Assignment
`\$x = 10;`
```\$x = 10;
\$x += 5;
// \$x = 15```
-= Subtraction assignment
```\$x = 10;
\$x -= 5;
// \$x = 5```
*= Multiplication assignment
```\$x = 10;
\$x *= 5;
// \$x = 50```
/= Division assignment
```\$x = 10;
\$x /= 5;
// \$x = 2```
%= Modulus assignment
```\$x = 10;
\$x %= 3;
// \$x = 1```

## Comparison Operators

Comparison operators are used to compare values. The following table shows the comparison operators in PHP:

Operator Description Example
== Equal to
```\$x = 10;
\$y = 10;
if (\$x == \$y) {
echo "x is equal to y";
}```
!= Not equal to
```\$x = 10;
\$y = 5;
if (\$x != \$y) {
echo "x is not equal to y";
}```
> Greater than
```\$x = 10;
\$y = 5;
if (\$x > \$y) {
echo "x is greater than y";
}```
< Less than
```\$x = 5;
\$y = 10;
if (\$x < \$y) {
echo "x is less than y";
}```
>= Greater than or equal to
```\$x = 10;
\$y = 10;
if (\$x >= \$y) {
echo "x is greater than or equal to y";
}```
<= Less than or equal to
```\$x = 5;
\$y = 10;
if (\$x <= \$y) {
echo "x is less than or equal to y";
}```

## More examples

Find more examples and operators on php.net.

Written by: Steven Iversen     25-05-2023     Written in: PHP tutorials