PHP Switch PHP

PHP Switch  

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

  1. switch(expression){      
  2. case value1:      
  3.  //code to be executed  
  4.  break;  
  5. case value2:      
  6.  //code to be executed  
  7.  break;  
  8. ......      
  9. default:       
  10.  code to be executed if all cases are not matched;    
  11. }  

PHP Switch Flowchart

flowchart of switch statement in php

PHP Switch Example

  1. <?php    
  2. $num=20;    
  3. switch($num){    
  4. case 10:    
  5. echo("number is equals to 10");    
  6. break;    
  7. case 20:    
  8. echo("number is equal to 20");    
  9. break;    
  10. case 30:    
  11. echo("number is equal to 30");    
  12. break;    
  13. default:    
  14. echo("number is not equal to 10, 20 or 30");    
  15. }   
  16. ?>    

Output:

number is equal to 20

Download free E-book of PHP


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries