Assignment Operators in PHP
Assignment operators in PHP are used to assign values to variables. These operators allow you to assign a specific value to a variable or assign the value of one variable to another.
The most common assignment operator in PHP is an equal (=) operator that can also be used together with various arithmetic operators. Assignment operators provide a compact and efficient way to write complex expressions.
We can categorize assignment operators into two types.
- Basic assignment operator
- Compound assignment operators
Basic Assignment Operator in PHP
The most basic assignment operator is equal character (=), which assigns the value on the right to the variable on the left. The general syntax to assign a value to a variable using assignment operator is as:
$variable = value;
Example 1:
<?php
$x = 10;
echo $x;
?>
Output: 10
In this example, we have assigned an integer type value to a variable named $x using the basic assignment operator. After that, we have displayed the result.
Example 2:
<?php
$name = "Tripti Priya";
echo $name;
?>
Output: Tripti Priya
In this example, we have assigned a string type value to a variable named $name and displayed the value of variable.
Compound Assignment Operators in PHP
Compound assignment operators allow you to perform an arithmetic operation and assignment in a single step. Instead of writing separate lines of code for performing operations and assigning the result, compound operators make the code more concise.
You can create compound assignment operators by combining the equal assignment operator with arithmetic operators. Following are the compound assignment operators in PHP.
- Addition Assignment (+=)
- Subtraction Assignment (-=)
- Multiplication Assignment (*=)
- Division Assignment (/=)
- Modulus Assignment (%=)
- Exponentiation Assignment (**=)
Addition Assignment (+=)
The addition assignment (+=) operator adds the right-hand value to the left-hand variable and assigns the result back to the left-hand variable. The general syntax is as follows:
$variable += value;
Example 3:
<?php
$a = 5;
$a += 10; // This is equivalent to $a = $a + 10;
echo $a;
?>
Output: 15
In this example, the line $a += 10; is equivalent to the expression $a = $a + 10;. The addition assignment operator (+=) adds the value 10 to the current value of $a (which is 5) and then assigns the result back to variable $a. Now the variable $a holds the value 15 which is displayed on the console.
Subtraction Assignment (-=)
Similar to the addition assignment operator +=, the subtraction assignment operator (-=) subtracts the right-hand value from the left-hand variable and assigns the result back to the left-hand variable. The general syntax is as:
$variable -= value;
Example 4:
<?php
$b = 20;
$b -= 5; // This expression is equivalent to $b = $b - 5;
echo $b;
?>
Output: 15
In this example, the line $b -= 5; is equivalent to the expression $b = $b + 20;. The subtraction assignment operator (-=) subtracts the value 5 to the current value of $b (which is 20) and then assigns the result back to variable $b. Now the variable $b holds the value 15 which is displayed on the console.
Multiplication Assignment (*=)
The multiplication assignment operator (*=) multiplies the left-hand variable by the right-hand value and assigns the result back to the variable. The general syntax for it is as follows:
$variable *= value;
Example 5:
<?php
$c = 40;
$c *= 5; // This equivalent to the expression $c = $c * 5;
echo $c;
?>
Output: 200
In this example, the line $c *= 5; is equivalent to the expression $c = $c * 5;. The multiplication assignment operator (*=) multiplies the value 5 to the current value of $c (which is 40) and then assigns the result back to variable named $c. Now the variable $c holds the value 200 which is displayed on the console.
Division Assignment (/=)
The division assignment operator (/=) divides the left-hand variable by the right-hand value and assigns the result back to the left-hand variable. The general syntax is as follows:
$variable /= value;
Example 6:
<?php
$d = 100;
$d /= 4; // This is equivalent to the expression $d = $d / 4;
echo $d;
?>
Output: 25
Modulus Assignment (%=)
The modulus assignment operator (%=) divides the left-hand variable by the right-hand value and assigns the remainder of the division back to the left-hand variable. The basic form of it is as:
$variable %= value;
Example 7:
<?php
$e = 27;
$e %= 4; // It is equivalent to $e = $e % 4;
echo $e;
?>
Output: 3
Exponentiation Assignment (**=)
The exponentiation assignment operator (**=) introduced in PHP 5.6 raises the left-hand variable to the power of the right-hand value and assigns the result back to the left-hand variable. The general form of it is as follows:
$variable **= value;
Example 8:
<?php
$f = 3;
$f **= 3; // It is equivalent to $f = $f ** 3;
echo $f;
?>
Output: 27
Summary of Assignment Operators in PHP
Here’s a summarized table of assignment operators in PHP, including both basic and compound assignment operators:
Operator | Operation | Example | Description |
---|---|---|---|
= | Assign | $a = $b; | The value of right operand is assigned to the left operand. |
+= | Add then assign | $a += $b; | Same as the $a = $a + $b; |
-= | Subtract then assign | $a -= $b; | Same as $a = $a - $b; |
*= | Multiply then assign. | $a *= $b; | Same as $a = $a * $b; |
/= | Divide then assign | $a /= $b; | Same as $a = $a / $b; |
%= | Divide then assign | $a %= $b; | Same as $a = $a % $b; |
**= | Raises then assign | $a **= $b; | Same as $a = $a ** $b; |
In addition to these operators, there is also string concatenation assignment operator (.=) and bitwise assignment operators which we will understand in further tutorial one by one. I hope that you will have understood the basic definition and syntax of all assignment operators.