PHP switch case statement

enables the execution of one or more statements when a specified expression's value matches a label.

A PHP switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label. If a match is found, the program executes the associated statement.

syntax:

         switch(expression){
    
                       case const_lable_1: statements;
                
                                  break;
                                              
                      case const_lable_2: statements;
                
                                  break;
                  
                      case const_lable_n: statements;
                
                                  break;
                                                              
                      default: statements;
                
                      }

the default and break is an optional.

case labels may end with a colon(:)

note : in a switch statement, the expression is evaluated only once and the result is compared to each case statement.


how switch statement work in PHP

  • first the value of the switch expression is compared with the values for each case in the structure.if there     is a match, the block of code associated with that case is executed.

  • if no case label is equal to the value of the switch expression,the statement associated with the default     label is executed.

  • the optional break statement associated with each case clause ensures that the program breaks out of     switch once the matched statement is executed and continues execution at the statement following switch.

  • if break is omitted, the program continues execution at the next statement in the switch statement.

    <?php   $x = 2;
    
          switch( $x ){
                   
                                 case 1 : echo  "x value is 1 <br/>" ;
                                 
                                 case 2 : echo  "x value is 2 <br/>" ;
                                 
                                 case 3 : echo  "x value is 3 <br/>" ;
                           }
      ?>
    <?php   $a = 2 ;
    
             switch( $a ){
                  
                             case  1 : echo "a value is 1 <br/>";
                      
                             case  2 : echo "a value is 2 <br/>"; break;
                      
                             case  3 : echo "a value is 3 <br/>";
                      
                             default: echo "x value  not matched with any case"; }
      ?>

what if switch expression value never match with any case value in switch structure.

   <?php  $name = "sandeep";
             
              switch( $name ){
             
                                     case  "ram" : echo "my name is ram";
                 
                                     case  "shyam" : echo "my name is shyam";
                 
                                     default : echo "no match found";
                                 }   
     ?>
<?php  $x = 2 ;  $y = 5;
   
       switch( $x ){
   
                          case  1 : echo "the x value is 1 <br/>";
                               
                          case  2 : echo "the x value is 2 <br/>";
                                    
                                        switch( $y ){
                                        
                                         case  4 : echo "inner switch  y value is 4 <br/>";
                                                  
                                         case  5 : echo "inner switch  y value is 5 <br/>";
                                                  
                                        }
                                             
                          case 3 : echo "the x value is 3"; break;
                               
                          default: echo "no match found";                               
                      }                          
    ?>

note : a switch statement with more than one default is valid in php 5.2.6 version . however, only the last default gets executed as the default clause.