0
0
PHPprogramming~15 mins

Switch statement execution in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Switch statement execution
📖 Scenario: You are creating a simple program to display the day of the week based on a number input. This is like a small calendar helper that tells you the name of the day when you enter a number from 1 to 7.
🎯 Goal: Build a PHP program that uses a switch statement to print the correct day name for a given number.
📋 What You'll Learn
Create a variable called dayNumber with a value from 1 to 7
Create a variable called defaultDay with the value 'Invalid day'
Use a switch statement on dayNumber with cases for numbers 1 to 7
Print the day name corresponding to the number (1 = Monday, 2 = Tuesday, ..., 7 = Sunday)
Print defaultDay if the number is not between 1 and 7
💡 Why This Matters
🌍 Real World
Switch statements are useful when you want to choose between many options based on a single value, like menu choices or day names.
💼 Career
Understanding switch statements helps in writing clear and efficient code for decision-making in many programming jobs.
Progress0 / 4 steps
1
Create the day number variable
Create a variable called dayNumber and set it to 3.
PHP
Need a hint?

Use $dayNumber = 3; to create the variable.

2
Create the default day variable
Create a variable called defaultDay and set it to the string 'Invalid day'.
PHP
Need a hint?

Use $defaultDay = 'Invalid day'; to create the variable.

3
Write the switch statement
Write a switch statement using $dayNumber to set a variable $dayName to the correct day name for numbers 1 to 7: 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday, 7 = Sunday. Use $defaultDay for any other number.
PHP
Need a hint?

Use switch ($dayNumber) { case 1: ... break; ... default: ... } to assign $dayName.

4
Print the day name
Print the value of the variable $dayName.
PHP
Need a hint?

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