Write a program in PHP to demonstrate the use of switch state and also show its output.
Question
Write a program in PHP to demonstrate the use of switch statement and also show its output.
Solution
Sure, here is a simple PHP program that demonstrates the use of a switch statement:
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Today is Monday.";
break;
case "Tuesday":
echo "Today is Tuesday.";
break;
case "Wednesday":
echo "Today is Wednesday.";
break;
case "Thursday":
echo "Today is Thursday.";
break;
case "Friday":
echo "Today is Friday.";
break;
case "Saturday":
echo "Today is Saturday.";
break;
case "Sunday":
echo "Today is Sunday.";
break;
default:
echo "Invalid day.";
}
?>
In this program, we have a variable $day
that holds the value "Monday". The switch statement checks the value of $day
. If the value matches one of the cases, it executes the code for that case. If it doesn't match any case, it executes the default case.
The break
keyword is used to prevent the code from running into the next case automatically.
The output of this program will be:
Today is Monday.
Similar Questions
a type of switch consisting of one input contact and one output contact. (input the abbreviation only)
Explain the various control structure with its syntax and example.a) If elseb) Switch casec) Break,continue and goto
When the switch is open, the data measured are listed below. Complete the table.(3 marks)Lamp L Lamp M Lamp NCurrent 0.3 AVoltage 1.5 V 0 V
Determine Output :void show() { printf("PISTA "; show();}void main() { printf("CACHEW "); return 10;}
Which of the following is used with the switch statement?Optionswhilecontinuebreakdo
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.