PHP - assignment operators example
The example below shows the usage of assignment and compound assignment operators:
- = Assignment operator
- += Addition AND assignment operator
- -= Subtraction AND assignment operator
- *= Multiply AND assignment operator
- /= Division AND assignment operator
- **= Exponent AND assignment operator
- %= Modulo AND assignment operator
<?php $a = 25; echo "a = $a\n"; //Addition AND assignment operator $a += 5; echo "\$a += 5; makes a = $a\n"; //Subtraction AND assignment operator $a -= 8; echo "\$a -= 8; makes a = $a\n"; //Multiply AND assignment operator $a *= 4; echo "\$a *= 4; makes a = $a\n"; //Division AND assignment operator $a /= 8; echo "\$a /= 8; makes a = $a\n"; //Exponent AND assignment operator $a **= 2; echo "\$a **= 2; makes a = $a\n"; //Modulo AND assignment operator //returns remainder of integer division $a %= 7; echo "\$a %= 7; makes a = $a\n"; ?>
The output of the above code will be:
a = 25 $a += 5; makes a = 30 $a -= 8; makes a = 22 $a *= 4; makes a = 88 $a /= 8; makes a = 11 $a **= 2; makes a = 121 $a %= 7; makes a = 2
❮ PHP - Operators