Assignment operators are used to assign values to variables. PHP offers shorthand assignment operators for performing operations and assigning the result in a single step.
= Assign+= Add and assign-= Subtract and assign*= Multiply and assign/= Divide and assign%= Modulus and assignAssignment operators combine arithmetic operations with assignment. They help reduce repetitive code and improve readability by updating variable values directly.
// Demonstrating PHP assignment operators step by step
<?php
$a = 10;
echo "Initial: $a<br>";
$a += 5;
echo "After += 5: $a<br>";
$a -= 3;
echo "After -= 3: $a<br>";
$a *= 2;
echo "After *= 2: $a<br>";
$a /= 4;
echo "After /= 4: $a<br>";
$a %= 3;
echo "After %= 3: $a<br>";
?>
This script updates the same variable multiple times using different assignment operators. Each operation modifies the current value of the variable and prints the updated result.
Change the starting value of $a to see how assignment operators update the result.
Think of assignment operators as shortcuts. Instead of rewriting the variable on both sides, PHP automatically applies the operation to the existing value.
assignment.php.