Switch statement in php

Switch case in php with multiple case example

Switch statement in php

Switch statement is used to execute one statement from multiple conditions in PHP. It works like PHP if-else-if statement. But it's better that if-else-if statement.

Syntax for Switch statement in php:

switch(condiction){      
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;    
}  

 

PHP Switch Example

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

 

Output:

your submited number is equal to 30
Top