Assignment Operators : PHP



PHP Assignment Operators :

Operator Description Example
= Assigns values from right side operands to left side operand Value of A + B is assigned to C
+= Adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A
-= Subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C – A
*= Multiply right operand to the left operand and assign the result to left operand C *= A is equivalent to C = C * A
/= Divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A
%= Divides left operand with the right operand and assign the remainder of division to left operand C %= A is equivalent to C = C % A

Example : Assignment Operators

<html>
<head><title>Assignment Operators</title><head>
<body>
<?php
    $num1 = 10;
    $num2 = 20;
    
    $res = $num1 + $num2;
    echo "Addtion Result: $res <br/>";
	
    $res += $num1;
    echo "Add & Assigment Result: $res <br/>";
    
	$res -= $num1;
    echo "Subtract & Assignment Result: $res <br/>";
    
	$res *= $num1;
    echo "Multiply & Assignment Result: $res <br/>";
    
	$res /= $num1;
    echo "Division & Assignment Result: $res <br/>";
    
	$res %= $num1;
    echo "Modulus & Assignment Result: $res <br/>";
?>

</body>
</html>

Output :

Addtion Result: 30 
Add & Assigment Result: 40 
Subtract & Assignment Result: 30 
Multiply & Assignment Result: 300 
Division & Assignment Result: 30 
Modulus & Assignment Result: 0