In the previous tutorials, if we discussed the if condition, if-else statements and nested if-else statements. In this tutorial, we will implement the switch case statement. As in nested if-else statements, you create many codes of blocks. It is very complex to code and a lot of conditions. To avoid this complexity and to avoid the code of block, we use switch statements. The switch statement is based on the case. The Switch case statements are easy to create rather then if-else and nested if-else statements.
You have to make a case in the switch case statement. The Switch statement is one of the best conditional structure in PHP.
Definition and use of the Switch Case Statements
Switch Case is used to avoid the blocks of if..else..elseif. code(Nested if-else).Switch Statement is used to execute one of many codes to be executed.
Switch Case Statements depend on Cases, If the case is matched, then the code of that case is executed and if any case does not match then the default code is executed, this is called switch case statement.
Syntax
switch (expression)
{
case 1:
code to be executed if the expression is matched
break;
case 2:
code to be executed if the expression is matched
break;
default:
code to be executed
if expression does not match with any case.
}
In the above syntax, you can see that if case 1 is occurred(matched) then the first statement will print(first code will execute). If the second case is occurred(matched) then the second statement will print(second code will execute) and if no case occurred(no matched) then default code will be executed.
Example of switch case statements
In the given example we create a variable. In that variable, we store value. After storing the value, that variable passes an expression in the switch. You can create a case according to the expression.
Example
<?php
$day="Fri";
switch ($day)
{
case "Sun":
echo "Today is Sunday";
break;
case "Wed":
echo "Today is Wednesday ";
break;
case "Tue":
echo "Today is Tuesday";
break;
case "Thu":
echo "Today is Thursday";
break;
case "Fri":
echo "Today is Friday";
break;
case "Sat":
echo "Today is Saturday";
break;
case "Sun":
echo "Today is Sunday";
break;
default:
echo "Which day is this ?";
}
?>
Run
In this above example, we created the switch Case for complete Week. In the variable, we have stored a variable value "Fri" as the string. Any case that matches the case expression will be executed. In case the Friday case will be executed, you store the Fri in the variable. Apart from this, if all cases do not match, the default code will be executed.
$day -- A variable.
Switch($day) -- Variable Passed in switch .
case "Sun":
echo "Today is Sunday"; ----First case (case number 1)
break;
case "Wed":
echo "Today is Wednesday "; ----Second case (case number 2)
break;
case "Tue":
echo "Today is Tuesday"; ----Third case (case number 3)
break;
case "Thu":
echo "Today is Thursday"; ----Fourth case (case number 4)
break;
case "Fri":
echo "Today is Friday"; ----Fifth case (case number 5)
break;
case "Sat":
echo "Today is Saturday"; ----Sixth case (case number 6)
break;
case "Sun":
echo "Today is Sunday"; ----Seventh case (case number 7)
break;
default:
echo "Which day is this ?"; ----(if no case matched then print another statement)
}
No case match with switch case expression
Now logic works if the case does not match. In the given example we created three variables. The first variable has a value 10 and the value of the second variable is 3 and the third variable has a value 78. We pass the third variable under the switch case expression. After that, we create cases. If no case matches with expression, so the default code will be executed.
Example
<?php
$a = 10;
$b =9;
$c=78;
switch($c){
case "a":
echo "a";
break;
case "b":
echo "b";
break;
default:
echo "Default code will be executed ";
break;
}
?>
Output
Default code will be executed
In the switch case statement, if the case is matched, the case code will be executed. if cases are not matched, the default code will be executed.
Recommended Posts:-