PHP Switch

PHP Switch

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;    
}  

Image Source : SPLessons


Example

<?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