Operators · Lesson 2 of 6
Assignment Operators
Assign values and use compound assignment for readable updates.
Lesson
The = operator assigns a value; it does not test equality. Compound operators such as += update a variable using its current value. Keep the longer form when it makes a complex calculation easier to understand, and avoid hiding important side effects inside a larger expression.
This running total uses a clear initialization followed by two incremental updates.
Further guidance
Evaluate an expression from its grouped parts and add parentheses whenever precedence is not obvious. Check divisors before division, be cautious with floating-point equality, and avoid changing state inside a complicated expression. Small, named intermediate values make calculations easier to verify.
Practice task: Create a fresh example for Assignment Operators. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.
Code examples
Assignment Operators
PHP
$score = 10;
$score += 5;
$score -= 2;
echo $score;
Expected output
13
Key points
- Assign values and use compound assignment for readable updates.
- Make calculation order explicit with parentheses and named intermediate values.
- Check edge cases such as zero divisors and floating-point precision.
Your learning progress
Finish this lesson
Optional practice
PHP playground
This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.
Execution is disabled. No code is sent to a server.