0
0
PHPprogramming~15 mins

Match expression (PHP 8) - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Match Expression in PHP 8
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print($dayName); to show the day name.