Bird
0
0

Identify the error in the following PHP code using the spaceship operator:

medium📝 Debug Q14 of 15
PHP - Operators
Identify the error in the following PHP code using the spaceship operator:
$a = 7;
$b = 3;
if ($a <=> $b) {
echo 'a is greater';
}
AVariables $a and $b are not defined
BSpaceship operator cannot be used in if condition
CMissing semicolon after echo statement
DThe condition always evaluates to true except when values are equal
Step-by-Step Solution
Solution:
  1. Step 1: Understand spaceship operator in if condition

    The operator returns -1, 0, or 1. In PHP, 0 is false, others are true.
  2. Step 2: Analyze the condition with $a=7 and $b=3

    Since 7 > 3, it returns 1, so condition is true and echo runs.
  3. Final Answer:

    The condition always evaluates to true except when values are equal -> Option D
  4. Quick Check:

    Non-zero spaceship result is true in if [OK]
Quick Trick: Spaceship returns 0 (false) only if values equal [OK]
Common Mistakes:
  • Thinking spaceship operator returns boolean
  • Expecting it to directly compare like > or <
  • Ignoring that 0 is false and others true in if

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes