PHP Assignment Operators

PHP Assignment operators in PHP are used to create and initialize a variable. The most basic assignment operator is the (=) sign, which you can use to assign and update the value of variables.

The lefthand side of a variable is always variable, and the right hand can be literal, complex expression, simple variable or function. Assignment operators supported by PHP are Assign(=),Add then assign(+=),Subtract then assign(-=),Multiply then assign(*),divide then assign(/=).

Operator Name Example Explanation
= Assign $m = $n The value of $n is assigned to $n
+= Add and Assign $m += $n It works similar to $m = $m + $n
-= Subtract and Assign $m -= $n Subtraction similar to $m = $m - $n
*= Multiply and Assign $m *= $n Multiplication similar to $m = $m * $n
/= Divide and Assign
(quotient)
$m /= $n Find quotient similar to $m = $m / $n
%= Divide and Assign
(remainder)
$m %= $n Find remainder similar to $m = $m % $n

PHP - Assign Operator(=):-

			

<?php
$a = 20;  
echo $a;
?> 

				

Output:

20

PHP - Add and assign Operator(+=):-

			

<?php
$a = 20;  
$a += 80;

echo $a;
?> 

				

Output:

100

PHP - Subtract and assign Operator(-=):-

			

<?php
$a = 100;  
$a -= 50;

echo $a;
?>  

				

Output:

50

PHP - Multiply and assign Operator(*=):-

			

<?php
$a = 4;
$a *= 6;

echo $a;
?>  

				

Output:

24

PHP - Divide and assign Operator(/=):-

			

<?php
$a = 20;
$a /= 5;

echo $a;
?>  

				

Output:

4