Operators · Lesson 3 of 6
Comparison Operators
Compare values with strict equality and ordered comparisons.
Lesson
Comparison operators return booleans. Prefer === and !== when both value and type should match; loose equality may coerce values and produce surprising results. Ordered comparisons such as = are useful for validated numeric ranges.
The example demonstrates that the integer 5 and string "5" are not identical.
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 Comparison 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
Comparison Operators
PHP
var_dump(5 === '5');
var_dump(5 == '5');
Expected output
bool(false) bool(true)
Key points
- Compare values with strict equality and ordered comparisons.
- 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.