The PHP assignment operators are used to write a value to a variable.
The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
Assignment | Same as... | Description |
---|---|---|
x = y | x = y | The left operand gets set to the value of the expression on the right |
x += y | x = x + y | Addition |
x -= y | x = x - y | Subtraction |
x *= y | x = x * y | Multiplication |
x /= y | x = x / y | Division |
x %= y | x = x % y | Modulus |
The example below shows the different results of using the different assignment operators:
<?php
$x=5;
echo $x; // outputs 5
$y=10;
$y += 50;
echo $y; // outputs 60
$z=40;
$z -= 20;
echo $z; // outputs 20
$i=2;
$i *=2;
echo $i; // outputs 4
$j=20;
$j /= 5;
echo $j; // outputs 4
$k=15;
$k %= 4;
echo $k; // outputs 3
?>
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning