PHP Assignment Operators - Assignment Operators in PHP



PHP Assignment Operators

  • PHP assignment operators are used to assign a value to a variable. They are the most common type of operator used in PHP.
  • Examples include the "=" operator, which assigns a value to a variable, and the "+=" operator, which adds a value to the variable and assigns the result.
Operator Syntax Operation
Assign (=) $a = $b Operand on the left obtains the value of the operand on the right
Add then Assign (+=) $a += $b Simple Addition same as $a = $a + $b
Subtract then Assign (-=) $a -= $b Simple subtraction same as $a = $a – $b
Multiply then Assign (*=) $a *= $b Simple product same as $x = $a * $b
Divide then Assign (quotient - /=) $a /= $b Simple division same as $x = $a / $b
Divide then Assign (remainder - %=) $a %= $b Simple division same as $x = $a % $b
php-assignment-operator

Sample Code

<?php
$b = 100;
echo $b, "<br>";
$b = 150;
$b += 200;
echo $b, "<br>";
$b = 25;
$b -= 10;
echo $b, "<br>";
$b = 10;
$b *= 20;
echo $b, "<br>";
$b = 200;
$b /= 5;
echo $b, "<br>";
$b = 500;
$b %= 5;
echo $b;
?>

Output

100
350
15
200
40
0

Related Searches to PHP Assignment Operators - Assignment Operators in PHP