PHP Switch
In PHP, the switch statement is a control structure that provides an efficient way to evaluate an expression and execute different blocks of code based on the value of that expression. It's often used when you have a single value and want to perform different actions depending on the possible values of that value. The switch statement is more concise and easier to read than a series of if-elseif statements when dealing with multiple conditions.
Syntax of switch statement:
switch (expression) {
case value1:
// Code to execute when expression equals value1
break;
case value2:
// Code to execute when expression equals value2
break;
// Additional cases as needed
default:
// Code to execute if none of the cases match
}
For example :
$day = "Monday";
switch ($day) {
case "Monday":
echo "It's the start of the workweek.";
break;
case "Friday":
echo "It's almost the weekend!";
break;
default:
echo "It's a regular day.";
}
Output:
In next chapter we start learning php forms , how to handle forms and much more
Share this page on :
© 2022 AnalyzeCode.com All rights reserved.