Bird
0
0

You want to categorize a number into 'small', 'medium', or 'large' using a PHP 8 match expression. Which code correctly implements this logic for:

hard📝 Application Q15 of 15
PHP - Conditional Statements
You want to categorize a number into 'small', 'medium', or 'large' using a PHP 8 match expression. Which code correctly implements this logic for:
- small: 1 or 2
- medium: 3 or 4
- large: any other number
$num = 3;
// Your match expression here
Amatch($num) { 1, 2 => 'small', 3, 4 => 'medium', _ => 'large' };
Bmatch($num) { 1, 2 => 'small', 3, 4 => 'medium', default => 'large' };
Cmatch $num { 1, 2 => 'small', 3, 4 => 'medium', default => 'large' };
Dmatch($num) { 1, 2: 'small', 3, 4: 'medium', default => 'large' };
Step-by-Step Solution
Solution:
  1. Step 1: Use comma-separated values for multiple cases

    PHP 8 match allows multiple values per case separated by commas.
  2. Step 2: Use correct default keyword

    Default fallback must use default keyword, not underscore or other.
  3. Final Answer:

    match($num) { 1, 2 => 'small', 3, 4 => 'medium', default => 'large' }; -> Option B
  4. Quick Check:

    Multiple values + default keyword = match($num) { 1, 2 => 'small', 3, 4 => 'medium', default => 'large' }; [OK]
Quick Trick: Use commas for multiple cases and default keyword for fallback [OK]
Common Mistakes:
  • Using _ instead of default for fallback
  • Not separating multiple cases with commas
  • Confusing syntax with switch statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes