Switch Case in PHP


Prev Tutorial Next Tutorial

Switch Case in PHP

In php switch statement is used to perform different actions based on different conditions. In others word PHP switch statement is used to execute one statement from multiple conditions. It works like PHP if-else-if statement.

Syntax

switch(expression){      
case value1:      
 //code to be executed  
 break;  
case value2:      
 //code to be executed  
 break;  
......      
default:       
 code to be executed if all cases are not matched;    
}  

Switch case Example in PHP

<?php

$num=5;    
switch($num){    
case 1:    
echo("Monday");    
break;    
case 2:    
echo("Tuesday");    
break;    
case 3:    
echo("Wednasday");    
break;
case 4:    
echo("Thrusday");    
break;
case 5:    
echo("Friday");    
break;
case 6:    
echo("Sateday");    
break;
case 7:    
echo("Sunday");    
break;

default:    
echo("Please enter number between 1 to 7");    
} 

?>

Output

Friday

Prev Tutorial Next Tutorial

Advertisements




Download Projects