Bird
0
0

Which of the following is the correct syntax for a match expression in PHP 8?

easy📝 Syntax Q3 of 15
PHP - Conditional Statements
Which of the following is the correct syntax for a match expression in PHP 8?
A$result = match ($value) { 1: 'one', 2: 'two', default: 'other' };
B$result = match ($value) { 1 => 'one'; 2 => 'two'; default => 'other'; };
C$result = match ($value) { 1 => 'one', 2 => 'two', default => 'other' };
D$result = match ($value) { 1 => 'one', 2 => 'two', else => 'other' };
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct arrow and separators

    Match uses => to associate cases and commas to separate them.
  2. Step 2: Confirm default keyword

    Default is specified as default => value, not else or with colons or semicolons.
  3. Final Answer:

    $result = match ($value) { 1 => 'one', 2 => 'two', default => 'other' }; -> Option C
  4. Quick Check:

    Match syntax = arrow + commas + default [OK]
Quick Trick: Use => and commas; default keyword is 'default' [OK]
Common Mistakes:
  • Using colons instead of arrows
  • Using semicolons instead of commas
  • Using 'else' instead of 'default'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes