your image

PHP Switch - javatpoint

javapoint
Related Topic
:- PHP Java CORE PHP

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

Important points to be noticed about switch case:

  1. The default is an optional statement. Even it is not important, that default must always be the last statement.
  2. There can be only one default in a switch statement. More than one default may lead to a Fatal error.
  3. Each case can have a break statement, which is used to terminate the sequence of statement.
  4. The break statement is optional to use in switch. If break is not used, all the statements will execute after finding matched case value.
  5. PHP allows you to use number, character, string, as well as functions in switch expression.
  6. Nesting of switch statements is allowed, but it makes the program more complex and less readable.
  7. You can use semicolon (;) instead of colon (:). It will not generate any error.

PHP Switch Flowchart

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

PHP switch statement with character

Program to check Vowel and consonant

We will pass a character in switch expression to check whether it is vowel or constant. If the passed character is A, E, I, O, or U, it will be vowel otherwise consonant.

 

 

  1. <?php      
  2.     $ch = 'U';  
  3.     switch ($ch)  
  4.     {     
  5.         case 'a':   
  6.             echo "Given character is vowel";  
  7.             break;  
  8.         case 'e':   
  9.             echo "Given character is vowel";  
  10.             break;  
  11.         case 'i':   
  12.             echo "Given character is vowel";  
  13.             break;  
  14.         case 'o':   
  15.             echo "Given character is vowel";  
  16.             break;    
  17.         case 'u':   
  18.             echo "Given character is vowel";  
  19.             break;  
  20.         case 'A':   
  21.             echo "Given character is vowel";  
  22.             break;  
  23.         case 'E':   
  24.             echo "Given character is vowel";  
  25.             break;  
  26.         case 'I':   
  27.             echo "Given character is vowel";  
  28.             break;  
  29.         case 'O':   
  30.             echo "Given character is vowel";  
  31.             break;  
  32.         case 'U':   
  33.             echo "Given character is vowel";  
  34.             break;  
  35.         default:   
  36.             echo "Given character is consonant";  
  37.             break;  
  38.     }  
  39. ?>    

Output:

Given character is vowel

PHP switch statement with String

PHP allows to pass string in switch expression. Let's see the below example of course duration by passing string in switch case statement.

  1. <?php      
  2.     $ch = "B.Tech";  
  3.     switch ($ch)  
  4.     {     
  5.         case "BCA":   
  6.             echo "BCA is 3 years course";  
  7.             break;  
  8.         case "Bsc":   
  9.             echo "Bsc is 3 years course";  
  10.             break;  
  11.         case "B.Tech":   
  12.             echo "B.Tech is 4 years course";  
  13.             break;  
  14.         case "B.Arch":   
  15.             echo "B.Arch is 5 years course";  
  16.             break;  
  17.         default:   
  18.             echo "Wrong Choice";  
  19.             break;  
  20.     }  
  21. ?>    

Output:

B.Tech is 4 years course

PHP switch statement is fall-through

PHP switch statement is fall-through. It means it will execute all statements after getting the first match, if break statement is not found.

  1. <?php      
  2.     $ch = 'c';  
  3.     switch ($ch)  
  4.     {     
  5.         case 'a':   
  6.             echo "Choice a";  
  7.             break;  
  8.         case 'b':   
  9.             echo "Choice b";  
  10.             break;  
  11.         case 'c':   
  12.             echo "Choice c";      
  13.             echo "</br>";  
  14.         case 'd':   
  15.             echo "Choice d";  
  16.             echo "</br>";  
  17.         default:   
  18.             echo "case a, b, c, and d is not found";  
  19.     }  
  20. ?>    

Output:

Choice cChoice dcase a, b, c, and d is not found

PHP nested switch statement

Nested switch statement means switch statement inside another switch statement. Sometimes it leads to confusion.

  1. <?php      
  2.     $car = "Hyundai";                   
  3.         $model = "Tucson";    
  4.         switch( $car )    
  5.         {    
  6.             case "Honda":    
  7.                 switch( $model )     
  8.                 {    
  9.                     case "Amaze":    
  10.                            echo "Honda Amaze price is 5.93 - 9.79 Lakh.";   
  11.                         break;    
  12.                     case "City":    
  13.                            echo "Honda City price is 9.91 - 14.31 Lakh.";    
  14.                         break;     
  15.                 }    
  16.                 break;    
  17.             case "Renault":    
  18.                 switch( $model )     
  19.                 {    
  20.                     case "Duster":    
  21.                         echo "Renault Duster price is 9.15 - 14.83 L.";  
  22.                         break;    
  23.                     case "Kwid":    
  24.                            echo "Renault Kwid price is 3.15 - 5.44 L.";  
  25.                         break;    
  26.                 }    
  27.                 break;    
  28.             case "Hyundai":    
  29.                 switch( $model )     
  30.                 {    
  31.                     case "Creta":    
  32.                         echo "Hyundai Creta price is 11.42 - 18.73 L.";  
  33.                         break;    
  34.         case "Tucson":    
  35.                            echo "Hyundai Tucson price is 22.39 - 32.07 L.";  
  36.                         break;   
  37.                     case "Xcent":    
  38.                            echo "Hyundai Xcent price is 6.5 - 10.05 L.";  
  39.                         break;    
  40.                 }    
  41.                 break;     
  42.         }  
  43. ?>    

Output:

Hyundai Tucson price is 22.39 - 32.07 L.

Comments