Bird
0
0

Identify the error in this PHP 8 match expression code:

medium📝 Debug Q14 of 15
PHP - Conditional Statements
Identify the error in this PHP 8 match expression code:
$input = 'banana';
$result = match($input) {
'apple' => 'fruit',
'carrot' => 'vegetable'
};
echo $result;
AMatch expression cannot use strings as cases.
BSyntax error due to missing semicolon after match.
CMissing default case causes UnhandledMatchError.
DVariable $result is not assigned properly.
Step-by-Step Solution
Solution:
  1. Step 1: Check for default case presence

    The match expression lacks a default case for unmatched values.
  2. Step 2: Understand consequences

    If input does not match any case, PHP throws UnhandledMatchError.
  3. Final Answer:

    Missing default case causes UnhandledMatchError. -> Option C
  4. Quick Check:

    Always include default to avoid errors [OK]
Quick Trick: Always add default case to handle unmatched values [OK]
Common Mistakes:
  • Assuming default case is optional
  • Thinking strings can't be used as match cases
  • Confusing syntax errors with runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes