The switch statement is similar to if..else statement only.
In many situation, you may need to check / compare different values with the same variable. In that situation switch statement really helps.
Switch case is bit faster compare with series of if statement.
<?php
$x = "announcement";
switch ($x) {
case "login":
echo "You are entered in Login / Signup page";
break;
case "home":
echo "You are entered in Home page";
break;
case "announcement":
echo "You are entered in Announcement page";
break;
case "logout":
echo "You are entered in Logout page";
break;
default:
echo "You are entered in Login / Signup page";
break;
}
?>
Output:
You are entered in Announcement
Note:
- break used to prevent the code running from current to next case level.
- In here you can able to see a special case called default.It is used to define the default value, if no statement match is found in the block segment.