Bird
0
0

Given the code:

hard📝 Application Q9 of 15
PHP - Operators
Given the code:
$arr = [1, 2, 3];
$result = $arr && false ? 'Yes' : 'No';
echo $result;

What is the output and why?
ANo, because the && operator evaluates to false
BNo output, script halts
CError, because arrays cannot be used with &&
DYes, because $arr is truthy and combined with false
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the && operator with array and false

    In PHP, non-empty arrays are truthy, but truthy && false is false.
  2. Step 2: Apply ternary operator

    Since the condition is false, the ternary returns 'No'.
  3. Final Answer:

    No, because the && operator evaluates to false -> Option A
  4. Quick Check:

    Truthy && false = false, ternary returns 'No' [OK]
Quick Trick: && returns false if any operand is false [OK]
Common Mistakes:
  • Assuming array && false is true
  • Thinking arrays cause errors with &&
  • Confusing ternary outputs
  • Ignoring operator precedence

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes