➕ PHP Operators
Estimated reading: 3 minutes 46 views

📥 PHP Assignment Operators Explained – =, +=, .=, **= and More


🧲 Introduction – Why Assignment Operators Matter in PHP

Assignment operators in PHP are essential for storing and manipulating values in variables. These operators not only assign values but can also perform operations while assigning, which reduces code repetition and improves efficiency.

🎯 In this guide, you’ll learn:

  • All types of PHP assignment operators
  • Syntax and purpose of each operator
  • Explained examples for each case
  • Best practices and usage tips

📘 What Are Assignment Operators in PHP?

Assignment operators are used to assign values to variables. The basic form uses the = symbol, but PHP provides compound assignment operators that combine arithmetic or bitwise operations with assignment.


📊 List of PHP Assignment Operators

OperatorNameDescriptionExampleEquivalent To
=AssignmentAssigns right operand to left variable$x = 5;
+=Add and assignAdds right to left and assigns result$x += 3;$x = $x + 3;
-=Subtract and assignSubtracts right from left and assigns result$x -= 2;$x = $x - 2;
*=Multiply and assignMultiplies and assigns result$x *= 4;$x = $x * 4;
/=Divide and assignDivides and assigns result$x /= 2;$x = $x / 2;
%=Modulus and assignAssigns remainder after division$x %= 3;$x = $x % 3;
.=Concatenate and assignAppends string to existing string$str .= "PHP";$str = $str . "PHP";
**=Exponentiation and assignRaises to power and assigns result$x **= 2;$x = $x ** 2;
&=Bitwise AND and assignBitwise AND with operand and assigns$x &= 5;$x = $x & 5;
`=`Bitwise OR and assignBitwise OR with operand and assigns`$x
^=Bitwise XOR and assignBitwise XOR with operand and assigns$x ^= 2;$x = $x ^ 2;
<<=Left shift and assignShifts bits left and assigns result$x <<= 1;$x = $x << 1;
>>=Right shift and assignShifts bits right and assigns result$x >>= 1;$x = $x >> 1;

🧪 Code Examples with Explanations

✅ Basic Assignment (=)

$score = 90;
echo $score;

📘 Explanation: Assigns the value 90 to the variable $score.


➕ Add and Assign (+=)

$x = 5;
$x += 10;
echo $x;  // Output: 15

📘 Explanation: Adds 10 to 5 and assigns the result (15) back to $x.


➖ Subtract and Assign (-=)

$points = 20;
$points -= 4;
echo $points;  // Output: 16

📘 Explanation: Subtracts 4 from 20 → result is 16.


✖ Multiply and Assign (*=)

$total = 6;
$total *= 3;
echo $total;  // Output: 18

📘 Explanation: 6 multiplied by 3 = 18


➗ Divide and Assign (/=)

$value = 50;
$value /= 5;
echo $value;  // Output: 10

📘 Explanation: 50 divided by 5 = 10


🔗 Concatenate and Assign (.=)

$greet = "Hello ";
$greet .= "World!";
echo $greet;  // Output: Hello World!

📘 Explanation: Appends "World!" to "Hello " using .= operator.


💥 Exponentiation and Assign (**=)

$num = 3;
$num **= 2;
echo $num;  // Output: 9

📘 Explanation: 3 squared (3^2) = 9


⚙ Bitwise Operators

$x = 6;      // 110 in binary
$x &= 3;     // 011 in binary
echo $x;     // Output: 2 (binary 010)

📘 Explanation: Performs bitwise AND between 6 and 3 → result is 2


🧠 Best Practices

  • ✅ Use compound operators (+=, .=, etc.) for cleaner, shorter code
  • ❌ Avoid using assignment inside conditions unless intentional: if ($x = 5) { ... } // This is assignment, not comparison
  • ✅ Use parentheses to group logic for clarity

📌 Summary – Recap & Next Steps

Assignment operators in PHP go beyond just =. Compound assignment operators like += and .= make your code more concise and expressive, especially for repetitive operations or string building.

🔍 Key Takeaways:

  • = assigns values, while operators like +=, *=, .=, etc. combine operations with assignment.
  • Use .= for string concatenation and **= for exponentiation (PHP 7+).
  • Bitwise assignment operators are useful in low-level bit manipulation.

⚙️ Real-World Relevance:
Used in counters, accumulators, math-based apps, string building, file processing, and game scoring systems.


❓ Frequently Asked Questions (FAQs)

❓ What’s the difference between = and == in PHP?
= is an assignment operator. == is a comparison operator.

❓ What does .= do in PHP?
✅ It appends (concatenates) the right-hand string to the left-hand variable.

❓ Are assignment operators chainable in PHP?
✅ Yes. Example: $x = $y = 10; sets both $x and $y to 10.

❓ Is **= available in all PHP versions?
✅ No, ** and **= were introduced in PHP 5.6 and PHP 7.0 respectively.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

📥 PHP Assignment Operators

Or Copy Link

CONTENTS
Scroll to Top