📖 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
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
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
Hint
Use switch ($dayNumber) { case 1: ... break; ... default: ... } to assign $dayName.
4
Print the day name
Print the value of the variable $dayName.
PHP
Hint
Use print($dayName); to show the day.
Practice
(1/5)
1. What does a switch statement do in PHP?
easy
A. It chooses which code to run based on a value.
B. It repeats code multiple times.
C. It defines a function.
D. It creates a new variable.
Solution
Step 1: Understand the purpose of switch
A switch statement compares a value to multiple cases to decide which code block to run.
Step 2: Compare with other options
Repeating code is done by loops, functions define reusable code, and variables store data, so these are not correct.
Final Answer:
It chooses which code to run based on a value. -> Option A
Quick Check:
Switch selects code by value = A [OK]
Hint: Switch picks code by matching value quickly [OK]
Common Mistakes:
Confusing switch with loops
Thinking switch defines functions
Believing switch creates variables
2. Which of the following is the correct syntax to start a switch statement in PHP?
easy
A. switch [variable] {
B. switch variable {
C. switch (variable) {
D. switch variable() {
Solution
Step 1: Recall switch syntax
In PHP, switch requires parentheses around the variable: switch (variable) {
Step 2: Check other options
Options without parentheses or with brackets or parentheses after variable name are invalid syntax.
Final Answer:
switch (variable) { -> Option C
Quick Check:
Switch uses parentheses around variable [OK]
Hint: Switch always uses parentheses around the variable [OK]