PHP - Assignment Operators

Assignment operators in PHP are used to assign values to variables. They not only store values but can also perform mathematical operations before assigning the final value.

PHP provides several types of assignment operators to simplify coding and improve efficiency. In this blog, we will explore different PHP assignment operators with practical examples.


List of PHP Assignment Operators

PHP offers the following assignment operators:

OperatorExampleEquivalent ToDescription
=$x = $y$x = $yAssigns the value of $y to $x
+=$x += $y$x = $x + $yAdds $y to $x and assigns the result to $x
-=$x -= $y$x = $x - $ySubtracts $y from $x and assigns the result to $x
*=$x *= $y$x = $x * $yMultiplies $x by $y and assigns the result to $x
/=$x /= $y$x = $x / $yDivides $x by $y and assigns the result to $x
%=$x %= $y$x = $x % $yCalculates $x modulus $y and assigns the remainder to $x
**=$x **= $y$x = $x ** $yRaises $x to the power of $y and assigns the result to $x

Examples of PHP Assignment Operators

1. Simple Assignment (=)

The basic assignment operator assigns the value of one variable to another.

<?php $x = 10; echo $x; // Output: 10 ?>

2. Addition Assignment (+=)

Adds a value to the variable and stores the result in the same variable.

<?php $x = 10; $x += 5; // Equivalent to $x = $x + 5; echo $x; // Output: 15 ?>

3. Subtraction Assignment (-=)

Subtracts a value from the variable and updates it.

<?php $x = 20; $x -= 5; // Equivalent to $x = $x - 5; echo $x; // Output: 15 ?>

4. Multiplication Assignment (*=)

Multiplies the variable by a value and stores the result.

<?php $x = 5; $x *= 4; // Equivalent to $x = $x * 4; echo $x; // Output: 20 ?>

5. Division Assignment (/=)

Divides the variable by a value and stores the result.

<?php $x = 40; $x /= 5; // Equivalent to $x = $x / 5; echo $x; // Output: 8 ?>

6. Modulus Assignment (%=)

Finds the remainder of division and assigns it to the variable.

<?php $x = 10; $x %= 3; // Equivalent to $x = $x % 3; echo $x; // Output: 1 ?>

7. Exponentiation Assignment (**=)

Raises a number to the power of another and assigns it to the variable.

<?php $x = 3; $x **= 2; // Equivalent to $x = $x ** 2; echo $x; // Output: 9 ?>

Use Cases of Assignment Operators in PHP

1. Updating a Counter

<?php $count = 0; $count += 1; echo "Current Count: " . $count; // Output: Current Count: 1 ?>

2. Accumulating a Total Price

<?php $total = 100; $price = 50; $total += $price; echo "Total Price: " . $total; // Output: Total Price: 150 ?>

3. Discount Calculation

<?php $price = 200; $discount = 20; $price -= $discount; echo "Price after discount: " . $price; // Output: Price after discount: 180 ?>

Key Takeaways

  • Assignment operators simplify variable updates in PHP.
  • The +=, -=, *=, /=, %= operators help modify variable values efficiently.
  • Exponentiation assignment (**=) is useful for power calculations.
  • These operators are widely used in loops, calculations, and real-time applications.

Conclusion

PHP assignment operators are fundamental for variable manipulation and arithmetic calculations. They make code cleaner, easier to read, and more efficient.

By understanding and practicing these operators, you can optimize your PHP programming skills and write better, more maintainable code.