Match expression (PHP 8) - Time & Space Complexity
Let's see how the time it takes to run a match expression changes as we add more cases.
We want to know how the number of cases affects the work done.
Analyze the time complexity of the following code snippet.
$value = 3;
switch ($value) {
case 1:
case 2:
$result = 'One or Two';
break;
case 3:
$result = 'Three';
break;
default:
$result = 'Other';
}
// Using match expression
$result = match ($value) {
1, 2 => 'One or Two',
3 => 'Three',
default => 'Other',
};
This code uses a match expression to select a result based on the value.
Look for repeated checks or comparisons.
- Primary operation: Comparing the input value to each case value.
- How many times: Up to the number of cases, until a match is found.
As the number of cases grows, the number of comparisons grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 comparisons |
| 100 | Up to 100 comparisons |
| 1000 | Up to 1000 comparisons |
Pattern observation: The work grows roughly in direct proportion to the number of cases.
Time Complexity: O(n)
This means the time to find a match grows linearly with the number of cases.
[X] Wrong: "Match expressions check all cases at once, so time stays the same no matter how many cases there are."
[OK] Correct: The match expression checks cases one by one until it finds a match, so more cases mean more checks.
Understanding how match expressions work helps you explain code efficiency clearly and shows you think about how code runs as it grows.
"What if the match expression used a hash map internally to find cases? How would the time complexity change?"
Practice
match expression in PHP 8 compared to a switch statement?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]
- Thinking match allows fall-through like switch
- Believing match works only with strings
- Assuming default case is mandatory in match
match expression in PHP 8?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]
- Using colon : instead of arrow =>
- Omitting parentheses around the match expression
- Using _ instead of default for fallback
$status = 2;
$message = match($status) {
1 => 'Pending',
2 => 'Approved',
3 => 'Rejected',
default => 'Unknown'
};
echo $message;
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]
- Confusing default case with matched case
- Assuming loose comparison might match wrong case
- Forgetting match returns a value, not prints it
$input = 'banana';
$result = match($input) {
'apple' => 'fruit',
'carrot' => 'vegetable'
};
echo $result;
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]
- Assuming default case is optional
- Thinking strings can't be used as match cases
- Confusing syntax errors with runtime errors
- small: 1 or 2
- medium: 3 or 4
- large: any other number
$num = 3;
// Your match expression here
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]
- Using _ instead of default for fallback
- Not separating multiple cases with commas
- Confusing syntax with switch statement
Ready to prove it?
Pass the test and this pattern moves unknown
