Bird
0
0

Identify the error in this PHP code using match:

medium📝 Debug Q6 of 15
PHP - Conditional Statements
Identify the error in this PHP code using match:
$x = 10;
$result = match ($x) {
5 => 'five',
10 => 'ten'
};
echo $result;
AMissing default case causes UnhandledMatchError if no match
BNo error, code runs fine
CMatch expression cannot be assigned to a variable
DSyntax error due to missing comma after last case
Step-by-Step Solution
Solution:
  1. Step 1: Check if default case is mandatory

    Default case is optional; here 10 matches a case, so no error.
  2. Step 2: Verify syntax correctness

    Trailing comma after last case is optional; missing comma is allowed.
  3. Final Answer:

    No error, code runs fine -> Option B
  4. Quick Check:

    Match without default is OK if a case matches [OK]
Quick Trick: Default case optional if all values covered [OK]
Common Mistakes:
  • Thinking trailing comma is mandatory
  • Assuming default case is required
  • Believing match cannot be assigned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes