Bird
Raised Fist0
PHPprogramming~20 mins

Match expression (PHP 8) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PHP 8 Match Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple match expression
What is the output of the following PHP code using a match expression?
PHP
<?php
$input = 2;
$result = match($input) {
    1 => 'One',
    2 => 'Two',
    3 => 'Three',
    default => 'Other'
};
echo $result;
ATwo
BOne
CThree
DOther
Attempts:
2 left
💡 Hint
Look at the value of $input and which case it matches.
❓ Predict Output
intermediate
2:00remaining
Match expression with multiple conditions
What will this PHP code output when using a match expression with multiple conditions in one arm?
PHP
<?php
$input = 'b';
$result = match($input) {
    'a', 'b', 'c' => 'Letter in a-c',
    'x', 'y', 'z' => 'Letter in x-z',
    default => 'Other letter'
};
echo $result;
ALetter in x-z
BOther letter
CLetter in a-c
DSyntax error
Attempts:
2 left
💡 Hint
Check which group the input letter belongs to.
❓ Predict Output
advanced
2:00remaining
Match expression with strict type comparison
What is the output of this PHP code using match expression with strict type comparison?
PHP
<?php
$input = '1';
$result = match($input) {
    1 => 'Integer one',
    '1' => 'String one',
    default => 'Other'
};
echo $result;
AInteger one
BTypeError
COther
DString one
Attempts:
2 left
💡 Hint
Match uses strict comparison (===), so type matters.
❓ Predict Output
advanced
2:00remaining
Match expression without default arm
What happens when this PHP code runs a match expression without a default arm and no case matches?
PHP
<?php
$input = 5;
$result = match($input) {
    1 => 'One',
    2 => 'Two',
    3 => 'Three'
};
echo $result;
AOne
BUnhandledMatchError
CThree
DTwo
Attempts:
2 left
💡 Hint
What if no case matches and no default is given?
🧠 Conceptual
expert
3:00remaining
Behavior of match expression with expressions as arms
Consider this PHP code using match expression with expressions as arms. What is the output?
PHP
<?php
$input = 3;
$result = match($input) {
    1 => 1 + 1,
    2 => 2 * 2,
    3 => strlen('abc'),
    default => 0
};
echo $result;
A3
B2
C4
D0
Attempts:
2 left
💡 Hint
Evaluate the expression for the matching case.

Practice

(1/5)
1. What is the main advantage of using a match expression in PHP 8 compared to a switch statement?
easy
A. It returns a value and uses strict comparison.
B. It allows fall-through between cases.
C. It only works with strings.
D. It requires a default case.

Solution

  1. Step 1: Understand match vs switch behavior

    Match expressions return a value directly, unlike switch which is a statement.
  2. Step 2: Identify comparison type

    Match uses strict comparison (===), while switch uses loose comparison (==).
  3. Final Answer:

    It returns a value and uses strict comparison. -> Option A
  4. Quick Check:

    Match returns value + strict check [OK]
Hint: Remember: match returns values and checks types strictly [OK]
Common Mistakes:
  • Thinking match allows fall-through like switch
  • Believing match works only with strings
  • Assuming default case is mandatory in match
2. Which of the following is the correct syntax for a simple match expression in PHP 8?
easy
A. match ($value) { 1 => 'one', 2 => 'two', default => 'other' };
B. match $value { 1: 'one', 2: 'two', default: 'other' };
C. match ($value) { 1: 'one', 2: 'two', default => 'other' };
D. match ($value) { 1 => 'one', 2 => 'two', _ => 'other' };

Solution

  1. Step 1: Recall match syntax

    Match uses parentheses around the expression and curly braces with arrows (=>) for cases.
  2. Step 2: Identify correct default syntax

    Default case uses the keyword default with =>, not _ or colon.
  3. Final Answer:

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

    Correct syntax uses parentheses, arrows, and default keyword [OK]
Hint: Use parentheses and arrows => with default keyword [OK]
Common Mistakes:
  • Using colon : instead of arrow =>
  • Omitting parentheses around the match expression
  • Using _ instead of default for fallback
3. What will be the output of the following PHP code?
$status = 2;
$message = match($status) {
1 => 'Pending',
2 => 'Approved',
3 => 'Rejected',
default => 'Unknown'
};
echo $message;
medium
A. Pending
B. Unknown
C. Rejected
D. Approved

Solution

  1. Step 1: Identify the value of $status

    $status is set to 2.
  2. Step 2: Match $status to cases

    Case 2 matches 'Approved'.
  3. Final Answer:

    Approved -> Option D
  4. Quick Check:

    Value 2 matches 'Approved' [OK]
Hint: Match returns the value for exact case match [OK]
Common Mistakes:
  • Confusing default case with matched case
  • Assuming loose comparison might match wrong case
  • Forgetting match returns a value, not prints it
4. Identify the error in this PHP 8 match expression code:
$input = 'banana';
$result = match($input) {
'apple' => 'fruit',
'carrot' => 'vegetable'
};
echo $result;
medium
A. Match expression cannot use strings as cases.
B. Syntax error due to missing semicolon after match.
C. Missing default case causes UnhandledMatchError.
D. Variable $result is not assigned properly.

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]
Hint: 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
5. 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
hard
A. match($num) { 1, 2 => 'small', 3, 4 => 'medium', _ => 'large' };
B. match($num) { 1, 2 => 'small', 3, 4 => 'medium', default => 'large' };
C. match $num { 1, 2 => 'small', 3, 4 => 'medium', default => 'large' };
D. match($num) { 1, 2: 'small', 3, 4: 'medium', default => 'large' };

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]
Hint: 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard