📖 Scenario: You are building a simple program to convert day numbers into day names. This is like a small calendar helper that tells you the name of the day when you give it a number.
🎯 Goal: Build a PHP program that uses the match expression to convert a day number (1 to 7) into the corresponding day name.
📋 What You'll Learn
Create a variable $dayNumber with a value between 1 and 7
Create a variable $dayName that uses the match expression with $dayNumber
Use match to return the correct day name string for each number from 1 to 7
Print the value of $dayName
💡 Why This Matters
🌍 Real World
Using <code>match</code> expressions helps write clear and concise code when you want to select one value from many options, like converting codes to names.
💼 Career
Understanding <code>match</code> expressions is useful for PHP developers to write modern, readable, and efficient conditional code.
Progress0 / 4 steps
1
Create the day number variable
Create a variable called $dayNumber and set it to the integer 3.
PHP
Hint
Use $dayNumber = 3; to create the variable.
2
Set up the match expression
Create a variable called $dayName and assign it the result of a match expression using $dayNumber.
PHP
Hint
Use match($dayNumber) { ... }; with cases for 1 to 7.
3
Complete the match cases
Complete the match expression so that each number from 1 to 7 returns the correct day name string: 1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday'.
PHP
Hint
Make sure all numbers 1 to 7 have a matching day name string.
4
Print the day name
Write a print statement to display the value of $dayName.
PHP
Hint
Use print($dayName); to show the day name.
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
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 A
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
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 keyword default with =>, not _ or colon.
Final Answer:
match ($value) { 1 => 'one', 2 => 'two', default => 'other' }; -> Option A
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?
B. Syntax error due to missing semicolon after match.
C. Missing default case causes UnhandledMatchError.
D. Variable $result is not assigned properly.
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 C
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