PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Assignment operators in PHP are used to assign values to variables. The most common assignment operator is the simple assignment operator =. However, PHP also provides several combined assignment operators, which perform an operation and assignment in one step. Here’s a detailed description of each assignment operator in PHP:

1. Simple Assignment Operator (=)

The simple assignment operator assigns the value on its right to the variable on its left.
				
					$variable = value;

				
			
Example:
				
					$x = 10;
$y = "Hello, World!";

				
			

2. Combined Assignment Operators

Combined assignment operators perform an arithmetic or bitwise operation and assign the result to the variable. Here are some of the most commonly used combined assignment operators:

a. Addition Assignment (+=)
Adds the right operand to the left operand and assigns the result to the left operand.
				
					$variable += value;

				
			
Example:
				
					$x = 5;
$x += 3; // $x is now 8

				
			
b. Subtraction Assignment (-=)
Subtracts the right operand from the left operand and assigns the result to the left operand.
				
					$variable += value;

				
			
Example:
				
					$x = 5;
$x -= 3; // $x is now 2

				
			
c. Multiplication Assignment (*=)
Multiplies the right operand with the left operand and assigns the result to the left operand.
				
					$variable *= value;

				
			
Example:
				
					$x = 5;
$x *= 3; // $x is now 15

				
			
d. Division Assignment (/=)
Divides the left operand by the right operand and assigns the result to the left operand.
				
					$variable /= value;

				
			
Example:
				
					$x = 10;
$x /= 2; // $x is now 5

				
			
e. Modulus Assignment (%=)
Calculates the modulus of the left operand by the right operand and assigns the result to the left operand.
				
					$variable %= value;

				
			
Example:
				
					$x = 10;
$x %= 3; // $x is now 1

				
			
f. Exponentiation Assignment (**=)
Raises the left operand to the power of the right operand and assigns the result to the left operand.
				
					$variable **= value;

				
			
Example:
				
					$x = 2;
$x **= 3; // $x is now 8

				
			

3. String Concatenation Assignment (.=)

Concatenates the right operand to the left operand and assigns the result to the left operand.
				
					$variable .= value;

				
			
Example:
				
					$str = "Hello";
$str .= " World"; // $str is now "Hello World"

				
			

4. Bitwise Assignment Operators

Bitwise assignment operators perform a bitwise operation and assign the result to the left operand.
a. AND Assignment (&=)
Performs a bitwise AND operation on the two operands and assigns the result to the left operand.
				
					$variable &= value;

				
			
Example:
				
					$x = 6; // 6 in binary is 110
$x &= 3; // 3 in binary is 011; $x is now 2 (binary 010)

				
			
b. OR Assignment (|=)
Performs a bitwise OR operation on the two operands and assigns the result to the left operand.
				
					$variable |= value;

				
			
Example:
				
					$x = 6; // 6 in binary is 110
$x |= 3; // 3 in binary is 011; $x is now 7 (binary 111)

				
			
c. XOR Assignment (^=)
Performs a bitwise XOR operation on the two operands and assigns the result to the left operand.
				
					$variable ^= value;

				
			
Example:
				
					$x = 6; // 6 in binary is 110
$x ^= 3; // 3 in binary is 011; $x is now 5 (binary 101)

				
			
d. Left Shift Assignment (<<=)
Shifts the left operand to the left by the number of bits specified by the right operand and assigns the result to the left operand.
				
					$variable <<= value;

				
			
Example:
				
					$x = 3; // 3 in binary is 011
$x <<= 2; // $x is now 12 (binary 1100)

				
			
e. Right Shift Assignment (>>=)
Shifts the left operand to the right by the number of bits specified by the right operand and assigns the result to the left operand.
				
					$variable >>= value;

				
			
Example:
				
					$x = 8; // 8 in binary is 1000
$x >>= 2; // $x is now 2 (binary 0010)

				
			

Assignment operators in PHP allow you to not only assign values to variables but also to perform arithmetic, string concatenation, and bitwise operations combined with assignment. Understanding these operators can make your code more concise and readable.

Scroll to Top