Operators are sings or symbols that are used to perform operations with variables and values. Like: +, -, =, ==, > etc.
<?php $x = 5; $y = 4; echo $x + $y; ?>
In the above example + plus is an operator and it is used to perform additions.
In PHP, there are so many operators, PHP divides these operators into the following groups –
This is a list of the most used operators. But in PHP, there are also more operators. Check out the list of all operators.
In this chapter, we will only discuss Arithmetic and Assignment operators. But in the next tutorials, we will discuss other operators briefly.
The PHP Arithmetic Operators are used to perform common math operations, such as addition, subtraction, multiplication, etc. You can also call these, mathematical operators.
$x = 6;
$y = 2;
PHP Assignment Operator is used to assign value to a variable. The assignment operator is the equal sign =.
<?php $x = "Hello PHP"; echo "$x"; ?>
Combined Assignment Operators are also used to assign values to variables.
But combined assignment operators first perform operations then assign values to variables.
<?php $x = 6; $x += 2; /* Same as $x = $x + 2; */ echo $x; ?>
In the above of example, first, we declare the $x variable with value 6, then in the second line, first, perform the addition operation $x + 2 and then we reassign the addition result into the $x variable.
List of PHP Combined Assignment Operators –
$x = 6;
<?php $x = 6; $x += 5; echo "Addition: $x <br>"; $x = 6; $x -= 2; echo "Subtraction: $x <br>"; $x = 6; $x *= 3; echo "Multiplication: $x <br>"; $x = 6; $x /= 2; echo "Division: $x <br>"; $x = 6; $x %= 3; echo "Modulus: $x <br>"; $x = 6; $x **= 2; echo "Exponentiation: $x"; ?>