PHP Operators: Assignment
The main assignment operator is ‘=’, which basically assigns a value on the right to a variable on the left. If you think this is the same as an equal sign in mathematics, don’t! Think of it as a ‘assigned to’ so whatever is on the right is assigned to the left.
Here’s a simple example:
1 2 | // this example assigns the number 6 to the variable 'apples' $apples = 6; |
There are a variety of ‘combined operators’ for all the binary arithmetic, array union and string operators that allows you to use a value in an expression and then assign its value to the result of that expression.
Here are the combined operators:
+= -= *= /= .= %= &= |= ^= <<= >>=
1 2 3 4 5 6 7 | // Using *= assignment $a = 3; $a *= 6; //is the same as doing $a = $a * 6. $a will be 18. // Using .= assignment $b = 'Hello '; $b .= ' There!'; // is the same as doing $b = $b . 'There!'; |
The rest of the combined operators are used in the same way.
Other similar posts:
- PHP Operators: Maths
- Introducing PHP Variables
- Parsing a URL querystring into variables in PHP
- Using regular expressions to extract content
- A look at the Google Graph class





