Operators · Lesson 4 of 6
Logical Operators
Combine boolean conditions with AND, OR, and NOT.
Lesson
Use && when every condition must be true, || when at least one must be true, and ! to negate a boolean. Parenthesize compound conditions so precedence is obvious. Short-circuit evaluation means the right side may not run if the left side already determines the result.
Authorization decisions should be expressed in terms of explicit, validated facts rather than loosely truthy input.
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 Logical 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
Logical Operators
PHP
$age = 20;
$hasTicket = true;
var_export($age >= 18 && $hasTicket);
Expected output
true
Key points
- Combine boolean conditions with AND, OR, and NOT.
- 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.