What if you could replace long, messy condition checks with a simple, elegant expression that does it all?
Why Match expression (PHP 8)? - Purpose & Use Cases
Imagine you have a list of colors, and you want to print a message for each color. You write many if or switch statements to check each color manually.
Using many if or switch statements is slow to write and easy to make mistakes. You might forget a case or write repeated code, making it hard to read and fix.
The match expression in PHP 8 lets you check values cleanly and return results directly. It is shorter, safer, and easier to understand than many if or switch cases.
switch ($color) {
case 'red':
$message = 'Stop';
break;
case 'green':
$message = 'Go';
break;
default:
$message = 'Wait';
}$message = match($color) {
'red' => 'Stop',
'green' => 'Go',
default => 'Wait',
};You can write clear, concise code that directly maps values to results without extra lines or errors.
In a web app, you can use match to quickly decide what message to show users based on their role, like 'admin', 'guest', or 'member', making your code neat and easy to update.
Manual checks with many if or switch are slow and error-prone.
Match expression simplifies value checking and result returning.
It makes code shorter, safer, and easier to read and maintain.
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
