Challenge - 5 Problems
Logical Operators Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of combined logical operators
What is the output of this PHP code snippet?
PHP
<?php $a = true; $b = false; $c = true; $result = $a && $b || $c; echo $result ? 'Yes' : 'No'; ?>
Attempts:
2 left
💡 Hint
Remember that && has higher precedence than || in PHP.
✗ Incorrect
The expression evaluates as ($a && $b) || $c, which is (true && false) || true, so false || true equals true, printing 'Yes'.
❓ Predict Output
intermediate2:00remaining
Logical operator precedence difference
What is the output of this PHP code?
PHP
<?php $a = false; $b = true; $c = false; $result = $a or $b and $c; echo $result ? 'True' : 'False'; ?>
Attempts:
2 left
💡 Hint
Remember that 'and' and 'or' have lower precedence than '=' in PHP.
✗ Incorrect
The assignment happens before 'or' and 'and'. So $result = $a (false), then 'or $b and $c' is evaluated but does not affect $result. So $result is false.
🔧 Debug
advanced2:00remaining
Identify the error in logical expression
Which option will cause a syntax error in PHP when using logical operators?
Attempts:
2 left
💡 Hint
Look for missing operands between operators.
✗ Incorrect
Option A has '&& ||' without a value between them, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Output of mixed logical operators with parentheses
What is the output of this PHP code?
PHP
<?php $a = false; $b = true; $c = true; $result = ($a or $b) and $c; echo $result ? 'Pass' : 'Fail'; ?>
Attempts:
2 left
💡 Hint
Parentheses change the order of evaluation.
✗ Incorrect
Inside parentheses: $a or $b is false or true = true. Then true and $c (true) is true, so output is 'Pass'.
🧠 Conceptual
expert2:00remaining
Understanding operator precedence and assignment
Given the code below, what is the value of $result after execution?
PHP
<?php $result = false; $flag = true; $result = $flag && false or true; ?>
Attempts:
2 left
💡 Hint
Remember that '&&' has higher precedence than 'or', but '=' has higher precedence than 'or'.
✗ Incorrect
The expression is evaluated as ($result = ($flag && false)) or true. So $result = false, then 'or true' is evaluated but does not affect $result. So $result remains false.