PHP Assignment Operators

In this lesson, you will learn about the Assignment Operators in PHP, their usage, and examples to better understand the topic.


PHP Assignment Operators

The assignment operator is the sign equal to “=.” it indicates that the left operand takes and allocates the value of the expression on the right operand. That is called value assigned. Such as the expression $x = 14 means assigning the value 14 to the variable x.

Below are the assignment Operations that PHP supports:

Assignment Operators Symbol Explanation Example
= a = b + c will allocate the value of b + c into
a
a = b + c
+= a += b is same to a = b + a a+=b
-= a -= b is same to a = a – b a-=b
*= a *= b is same to a = a * b a*=b
/= a /= b is same to a = a / b a/=b
%= a %= b is same to a = a % b a%=b
.= a .= b is same to a = a . b a.=b
&= a &= b is same to a = a & b a&=b
|= a |= b is same to a = a | b a|=b
^= a ^= b is same to a = a ^ b a^=b
<<= a <<= b is same to a = a << b a <<= b
>>= a >>= b is same to a = a >> b a >>= b

Example

<?PHP
echo "PHP Assignment Operator Example";
echo "<br>";
echo "  ---------------------------------------- ";
echo "<br>";
$e = "";
$d = "Great tutorial Thank you!!!  ";

//Concatenation Assignment operator allocate values from right side operands to left side operand
$e .= $d;
echo "<br>";
echo '$d =', $d;

echo "<br>";
echo " Concatenation operation-> ", '.=';
echo "<br>";
echo "Expression -> ", '$e .= $d';
echo "<br>";
echo "value of ", $d, " allocated to ", $e;
echo "<br>";

//Add and += operator adds right operand to the left operand and allocate the result to left operand
echo "  ---------------------------------------- ";
echo "<br>";
$a = 84;
$c = 0;
echo '$a  = ', $a, "<br>", '$c  = ', $c;
echo "<br>";
$c += $a;

echo " Addition and operation-> ", '+=';
echo "<br>";
echo "Expression -> ", '$c += $a';
echo "<br>";
echo "value of ", '$a', " added to ", '$c', " allocated to ", '$c';
echo "<br>";
echo "Result -> ", $c;
echo "<br>";
$a = 84;
$c = 0;

//Subtract and -= operator subtracts the right operand to the left operand and allocates the result to the left operand
echo "  ---------------------------------------- ";
echo "<br>";
$a = 3;
$c = 83;
echo '$a  = ', $a, "<br>", '$c  = ', $c;
echo "<br>";
$c -= $a;
echo " Subtract and operation-> ", '-=';
echo "<br>";
echo "Expression -> ", '$c -= $a';
echo "<br>";
echo "value of ", '$a', " subtracted to ", '$c', " allocated to ", '$c';
echo "<br>";
echo "Result -> ", $c;
echo "<br>";

//Multiply and -= operator multiply right operand to the left operand and allocate the result to left operand
echo "  ---------------------------------------- ";
echo "<br>";
$a = 11;
$c = 3;
echo '$a  = ', $a, "<br>", '$c  = ', $c;
echo "<br>";
$c *= $a;
echo " Multiply and operation-> ", '*=';
echo "<br>";
echo "Expression -> ", '$c *= $a';
echo "<br>";
echo "value of ", '$a', " multiplied to ", '$c', " allocated to ", '$c';
echo "<br>";
echo "Result -> ", $c;
echo "<br>";
$a = 84;
$c = 0;
//Division and /= operator divide right operand to the left operand and allocate the result to left operand
echo "  ---------------------------------------- ";
echo "<br>";
$a = 5;
$c = 10;
echo '$a  = ', $a, "<br>", '$c  = ', $c;
echo "<br>";
echo " Division and operation-> ", '/=';
echo "<br>";
$c /= $a;
echo "Expression -> ", '$c /= $a';
echo "<br>";
echo "value of ", '$a', " divided to ", '$c', " allocated to ", '$c';
echo "<br>";
echo "Result -> ", $c;
echo "<br>";

//Modulus and %= operator modulus right operand to the left operand and allocate the result to left operand
echo "  ---------------------------------------- ";
echo "<br>";
$a = 5;
$c = 3;
echo '$a  = ', $a, "<br>", '$c  = ', $c;
echo "<br>";
echo " Modulus and operation-> ", '%=';
$c %= $a;
echo "<br>";
echo "Expression -> ", '$c %= $a';
echo "<br>";
echo "value of ", '$a', " modulus to ", '$c', " allocated to ", '$c';
echo "<br>";
echo "Result -> ", $c;
echo "<br>";
?>

Output

PHP Assignment Operator Example
---------------------------------------- 

$d =Great tutorial Thank you!!! 
Concatenation operation-> .=
Expression -> $e .= $d
value of Great tutorial Thank you!!! allocated to Great tutorial Thank you!!! 
---------------------------------------- 
$a = 84
$c = 0
Addition and operation-> +=
Expression -> $c += $a
value of $a added to $c allocated to $c
Result -> 84
---------------------------------------- 
$a = 3
$c = 83
Subtract and operation-> -=
Expression -> $c -= $a
value of $a subtracted to $c allocated to $c
Result -> 80
---------------------------------------- 
$a = 11
$c = 3
Multiply and operation-> *=
Expression -> $c *= $a
value of $a multiplied to $c allocated to $c
Result -> 33
---------------------------------------- 
$a = 5
$c = 10
Division and operation-> /=
Expression -> $c /= $a
value of $a divided to $c allocated to $c
Result -> 2
---------------------------------------- 
$a = 5
$c = 3
Modulus and operation-> %=
Expression -> $c %= $a
value of $a modulus to $c allocated to $c
Result -> 3

This concludes the PHP Assignment Operators lesson. In the next lesson, you will learn about PHP Conditional Operators in PHP.