The switch Statement

The switch Statement:
The switch statement is useful in cases in which one variable or the result of an expression can have multiple values, which should each trigger a different function.
For example, consider a PHP driven menu system that passes a single string to the main menu code according to what the user requests. Let’s say the options are Homes, About, News, login, and Links and we get the variable $page to one of these, according to user input.

Switch Case
Switch Case
1.Example for Switch statement:
 
<?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 "Wonder which day is this ?";
         }
      ?>
OUTPUT:
Today is Monday
Example for Switch Statement:

<?php    
$num=20;    
switch($num){    
case 10:    
echo("number is equals to 10");    
break;    
case 20:    
echo("number is equal to 20");    
break;    
case 30:    
echo("number is equal to 30");    
break;    
default:    
echo("number is not equal to 10, 20 or 30");    
}   
?>    

Output:
number is equal to 20 
Translate »