Assignment operators in PHP are used to assign values to variables.
The simplest assignment operator is the = operator, but PHP provides several compound assignment operators that combine basic operations (like addition or subtraction) with assignment.
In this tutorial, we will cover:
Table of Contents
Let’s explore each assignment operator with examples and explanations.
1. Basic Assignment (=)
The basic assignment operator (=) is used to assign a value to a variable. It simply stores the right-hand side value in the variable on the left-hand side.
Syntax:
$variable = value;
Example:
<?php $x = 10; // Assigns the value 10 to $x $y = "Hello, World!"; // Assigns a string value to $y echo $x; // Outputs: 10 echo $y; // Outputs: Hello, World! ?>
- In this example, $x is assigned the value 10, and $y is assigned the string “Hello, World!”.
2. Addition Assignment (+=)
The addition assignment operator (+=) adds the right-hand operand to the left-hand variable and stores the result in the left-hand variable.
Syntax:
$variable += value;
Example:
<?php $x = 10; $x += 5; // Same as $x = $x + 5 echo $x; // Outputs: 15 ?>
- In this example, $x is first assigned the value 10, and then 5 is added to $x, resulting in $x = 15.
3. Subtraction Assignment (-=)
The subtraction assignment operator (-=) subtracts the right-hand operand from the left-hand variable and stores the result in the left-hand variable.
Syntax:
$variable -= value;
Example:
<?php $x = 20; $x -= 5; // Same as $x = $x - 5 echo $x; // Outputs: 15 ?>
- Here, $x starts with the value 20, and 5 is subtracted from it, leaving $x = 15.
4. Multiplication Assignment (*=)
The multiplication assignment operator (*=) multiplies the left-hand variable by the right-hand operand and stores the result in the left-hand variable.
Syntax:
$variable *= value;
Example:
<?php $x = 5; $x *= 3; // Same as $x = $x * 3 echo $x; // Outputs: 15 ?>
- In this case, $x is multiplied by 3, resulting in $x = 15.
5. Division Assignment (/=)
The division assignment operator (/=) divides the left-hand variable by the right-hand operand and stores the result in the left-hand variable.
Syntax:
$variable /= value;
Example:
<?php $x = 20; $x /= 4; // Same as $x = $x / 4 echo $x; // Outputs: 5 ?>
- $x is divided by 4, resulting in $x = 5.
6. Modulus Assignment (%=)
The modulus assignment operator (%=) divides the left-hand variable by the right-hand operand and stores the remainder in the left-hand variable.
Syntax:
$variable %= value;
Example:
<?php $x = 10; $x %= 3; // Same as $x = $x % 3 echo $x; // Outputs: 1 ?>
- The modulus operation gives the remainder when $x is divided by 3. In this case, the remainder is 1, so $x = 1.
7. Concatenation Assignment (.=)
The concatenation assignment operator (.=) appends the right-hand string to the left-hand variable and stores the result in the left-hand variable.
Syntax:
$variable .= value;
Example:
<?php $greeting = "Hello"; $greeting .= ", World!"; // Same as $greeting = $greeting . ", World!" echo $greeting; // Outputs: Hello, World! ?>
- The string “, World!” is appended to $greeting, resulting in “Hello, World!”.
8. Bitwise Assignment Operators
PHP also supports assignment operators that work with bitwise operations. Bitwise operators allow you to manipulate individual bits within an integer.
AND Assignment (&=)
The AND assignment operator (&=) performs a bitwise AND operation between the left-hand variable and the right-hand operand.
Example:
<?php $x = 6; // 6 in binary: 110 $x &= 3; // 3 in binary: 011, Result: 010 (2 in decimal) echo $x; // Outputs: 2 ?>
- The bitwise AND operation results in 2.
OR Assignment (|=)
The OR assignment operator (|=) performs a bitwise OR operation between the left-hand variable and the right-hand operand.
Example:
<?php $x = 6; // 6 in binary: 110 $x |= 3; // 3 in binary: 011, Result: 111 (7 in decimal) echo $x; // Outputs: 7 ?>
- The bitwise OR operation results in 7.
XOR Assignment (^=)
The XOR assignment operator (^=) performs a bitwise XOR operation between the left-hand variable and the right-hand operand.
Example:
<?php $x = 6; // 6 in binary: 110 $x ^= 3; // 3 in binary: 011, Result: 101 (5 in decimal) echo $x; // Outputs: 5 ?>
- The bitwise XOR operation results in 5.
Left Shift Assignment (<<=)
The left shift assignment operator (<<=) shifts the bits of the left-hand variable to the left by the number of positions specified by the right-hand operand.
Example:
<?php $x = 3; // 3 in binary: 011 $x <<= 2; // Shift left by 2 bits, Result: 1100 (12 in decimal) echo $x; // Outputs: 12 ?>
- Shifting the bits of 3 to the left by 2 results in 12.
Right Shift Assignment (>>=)
The right shift assignment operator (>>=) shifts the bits of the left-hand variable to the right by the number of positions specified by the right-hand operand.
Example:
<?php $x = 12; // 12 in binary: 1100 $x >>= 2; // Shift right by 2 bits, Result: 0011 (3 in decimal) echo $x; // Outputs: 3 ?>
- Shifting the bits of 12 to the right by 2 results in 3.
Summary of PHP Assignment Operators:
Operator | Description | Example | Equivalent To |
---|---|---|---|
= | Basic assignment, assigns a value to a variable | $x = 10; | |
+= | Adds the right-hand value to the left-hand variable | $x += 5; | $x = $x + 5; |
-= | Subtracts the right-hand value from the left-hand variable | $x -= 5; | $x = $x – 5; |
*= | Multiplies the left-hand variable by the right-hand value | $x *= 5; | $x = $x * 5; |
/= | Divides the left-hand variable by the right-hand value | $x /= 5; | $x = $x / 5; |
%= | Modulus (remainder) assignment | $x %= 3; | $x = $x % 3; |
.= | Concatenates the right-hand string to the left-hand variable | $str .= ‘World!’; | $str = $str . ‘World!’; |
&= | Performs a bitwise AND and assigns the result | $x &= 3; | $x = $x & 3; |
|= | Performs a bitwise OR and assigns the result | $x |= 3 | $x = $x | 3 |
^= | Performs a bitwise XOR and assigns the result | $x ^= 3; | $x = $x ^ 3; |
<<= | Left shift assignment (shifts bits to the left) | $x <<= 2; | $x = $x << 2; |
>>= | Right shift assignment (shifts bits to the right) | $x >>= 2; | $x = $x >> 2; |
Conclusion
Assignment operators in PHP allow you to perform operations and assign values to variables in a single step. Whether you’re performing arithmetic, concatenating strings, or manipulating bits, PHP provides a variety of assignment operators to make your code more efficient. In this tutorial, we covered:
- Basic assignment with the = operator.
- Compound assignment for arithmetic operations (+=, -=, etc.).
- Concatenation assignment with .= for strings.
- Bitwise assignment operators for advanced bit manipulation.
By mastering these operators, you can write cleaner, more concise PHP code for various tasks.