PHP Switch Case
In the last chapter we have studied if-else statement and elseif statement in PHP. In this chapter we will be learning the PHP switch case statement.
PHP Switch Case :
In order to avoid the confusing multiple and nested if blocks it is advisable to use switch case. Consider the following switch case statement syntax -
switch (expression) { case label1: executed if expression = label1; break; case label2: executed if expression = label2; break; case label3: executed if expression = label3; break; default: executed if expression != any label; break; }
Example of Switch case Statement :
<html> <body> <?php $d=date("D"); switch ($d) { case "Mon": echo "Today is Monday"; break; case "Tue": echo "Today is Tuesday"; break; case "Wed": echo "Today is Wednesday"; 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 "Invalid day"; } ?> </body> </html>
Output :
Today is Sunday