The match expression helps you choose a value based on a condition, like a smarter and cleaner switch-case. It makes your code easier to read and write.
Match expression (PHP 8)
Introduction
Syntax
PHP
match (expression) {
value1 => result1,
value2, value3 => result2,
default => defaultResult,
};The match expression returns a value directly.
It uses strict comparison (===), so types must match exactly.
Examples
PHP
$result = match ($color) {
'red' => 'Stop',
'yellow' => 'Caution',
'green' => 'Go',
default => 'Unknown color',
};PHP
$grade = 85; $message = match (true) { $grade >= 90 => 'Excellent', $grade >= 75 => 'Good', $grade >= 60 => 'Pass', default => 'Fail', };
PHP
$day = 3; $dayName = match ($day) { 1, 7 => 'Weekend', 2, 3, 4, 5, 6 => 'Weekday', default => 'Invalid day', };
Sample Program
This program uses a match expression to print a message based on the score value.
PHP
<?php $score = 72; $message = match (true) { $score >= 90 => 'Great job!', $score >= 70 => 'Good effort!', $score >= 50 => 'You passed.', default => 'Try again.', }; echo $message;
Important Notes
Match expressions use strict comparison (===), so '1' (string) and 1 (int) are different.
Every match arm must return a value; no fall-through like switch.
Match expressions always return a value, so you can assign them directly to variables.
Summary
Match expressions simplify choosing values based on conditions.
They return values directly and use strict comparison.
They help write cleaner and less error-prone code than switch-case.
Practice
1. What is the main advantage of using a
match expression in PHP 8 compared to a switch statement?easy
Solution
Step 1: Understand match vs switch behavior
Match expressions return a value directly, unlike switch which is a statement.Step 2: Identify comparison type
Match uses strict comparison (===), while switch uses loose comparison (==).Final Answer:
It returns a value and uses strict comparison. -> Option AQuick 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
Solution
Step 1: Recall match syntax
Match uses parentheses around the expression and curly braces with arrows (=>) for cases.Step 2: Identify correct default syntax
Default case uses the keyworddefaultwith =>, not _ or colon.Final Answer:
match ($value) { 1 => 'one', 2 => 'two', default => 'other' }; -> Option AQuick 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
Solution
Step 1: Identify the value of $status
$status is set to 2.Step 2: Match $status to cases
Case 2 matches 'Approved'.Final Answer:
Approved -> Option DQuick 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
Solution
Step 1: Check for default case presence
The match expression lacks a default case for unmatched values.Step 2: Understand consequences
If input does not match any case, PHP throws UnhandledMatchError.Final Answer:
Missing default case causes UnhandledMatchError. -> Option CQuick 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
- small: 1 or 2
- medium: 3 or 4
- large: any other number
$num = 3;
// Your match expression here
hard
Solution
Step 1: Use comma-separated values for multiple cases
PHP 8 match allows multiple values per case separated by commas.Step 2: Use correct default keyword
Default fallback must usedefaultkeyword, not underscore or other.Final Answer:
match($num) { 1, 2 => 'small', 3, 4 => 'medium', default => 'large' }; -> Option BQuick 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
10 questions ~5 mineasy hard
