PHP Assignment Operators
Summary: in this tutorial, you will learn about the most commonly used PHP assignment operators.
Introduction to the PHP assignment operator
PHP uses the = to represent the assignment operator. The following shows the syntax of the assignment operator:
$variable_name = expression;Code language: PHP (php)
On the left side of the assignment operator (=) is a variable to which you want to assign a value. And on the right side of the assignment operator (=) is a value or an expression.
When evaluating the assignment operator (=), PHP evaluates the expression on the right side first and assigns the result to the variable on the left side. For example:
$x = 10;
$y = 20;
$total = $x + $y;Code language: PHP (php)
In this example, we assigned 10 to $x, 20 to $y, and the sum of $x and $y to $total.
The assignment expression returns a value assigned, which is the result of the expression in this case:
$variable_name = expression;Code language: PHP (php)
It means that you can use multiple assignment operators in a single statement like this:
$x = $y = 20;Code language: PHP (php)
In this case, PHP evaluates the right-most expression first:
$y = 20Code language: PHP (php)
The variable $y is 20.
The assignment expression $y = 20 returns 20 so PHP assigns 20 to $x. After the assignments, both $x and $y equal 20.
Arithmetic assignment operators
Sometimes, you want to increase a variable by a specific value. For example:
$counter = 1;
$counter = $counter + 1;Code language: PHP (php)
How it works.
- First,
$counteris set to1. - Then, increase the
$counterby1and assign the result to the$counter.
After the assignments, the value of $counter is 2.
PHP provides the arithmetic assignment operator += that can do the same but with a shorter code. For example:
$counter = 1;
$counter += 1;Code language: PHP (php)
The expression $counter += 1 is equivalent to the expression $counter = $counter + 1.
Besides the += operator, PHP provides other arithmetic assignment operators. The following table illustrates all the arithmetic assignment operators:
| Operator | Example | Equivalent | Operation |
|---|---|---|---|
| += | $x += $y | $x = $x + $y | Addition |
| -= | $x -= $y | $x = $x – $y | Subtraction |
| *= | $x *= $y | $x = $x * $y | Multiplication |
| /= | $x /= $y | $x = $x / $y | Division |
| %= | $x %= $y | $x = $x % $y | Modulus |
| **= | $z **= $y | $x = $x ** $y | Exponentiation |
Concatenation assignment operator
PHP uses the concatenation operator (.) to concatenate two strings. For example:
$greeting = 'Hello ';
$name = 'John';
$greeting = $greeting . $name;
echo $greeting;
Code language: HTML, XML (xml)
Output:
Hello John
By using the concatenation assignment operator you can concatenate two strings and assigns the result string to a variable. For example:
$greeting = 'Hello ';
$name = 'John';
$greeting .= $name;
echo $greeting;
Code language: HTML, XML (xml)
Summary
- Use PHP assignment operator (
=) to assign a value to a variable. The assignment expression returns the value assigned. - Use arithmetic assignment operators to carry arithmetic operations and assign at the same time.
- Use concatenation assignment operator (
.=)to concatenate strings and assign the result to a variable in a single statement.