PHP Tutorial - PHP switch






To test an expression against a range of different values, and does a different task depending on the matched value.

Syntax

In a switch/case block, you specify what you are checking against, then give a list of possible values you want to handle.

switch(value){
   case constant_1:
         do if the value is constant_1
         break;
   case constant_2:
         do if the value is constant_2
         break;
   case constant_3:
         do if the value is constant_3
         break;
   default:
         do if no one matched
         break;
}




Note

Each case construct has a break statement at the end of it. break exits the entire switch construct, ensuring that no more code blocks within the switch construct are run.

Example

The following code uses the switch statement to check string value.


<?php/* w w  w.j  a va 2 s  .com*/
        $Name = 'Bob';
        switch($Name) {
        case "Jack":
                print "Your name is Jack\n";
                break;
        case "Linda":
                print "Your name is Linda\n";
                break;
        case "Bob":
                print "Your name is Bob\n";
                break;
        default:
                print "I don't know your name!\n";
        }
?>

The code above generates the following result.





Example 2

The following code shows how to cover all cases with switch.


<?php/*w w w. jav a2 s. c o  m*/
  /*
  ** Get today's weekday name
  */
  $englishDay = date("l");

  /*
  ** Find the today's German name
  */
  switch($englishDay)
  {
    case "Monday":
      $deutschDay = "Montag";
      break;
    case "Tuesday":
      $deutschDay = "Dienstag";
      break;
    case "Wednesday":
      $deutschDay = "Mittwoch";
      break;
    case "Thursday":
      $deutschDay = "Donnerstag";
      break;
    case "Friday":
      $deutschDay = "Freitag";
      break;
    case "Saturday":
      $deutschDay = "Samstag";
      break;
    default:
      // It must be Sunday
      $deutschDay = "Sonntag";
  }

  /*
  ** Print today's English and German names
  */
  print("<h2>German Lesson: Day of the Week</h2>\n" .
    "<p>\n" .
    "In English: <b>$englishDay</b>.<br>\n" .
    "In German: <b>$deutschDay</b>\n" .
    "</p>\n");
?>

The code above generates the following result.

Example 3

The following code shows how to switch with fall through.


<!DOCTYPE html>//from   w  w  w. j  a v a  2  s  . c o  m
<html>
  <body>
    <table border="1">
      <tr>
        <th>Number</th>
        <th>Odd or Even?</th>
        <th>Prime?</th>
      </tr>
<?php

for ( $i = 1; $i <= 10; $i++ ) {
  $oddEven = ( $i % 2 == 0 ) ? "Even" : "Odd";
  switch ( $i ) {
    case 2:
    case 3:
    case 5:
    case 7:
      $prime = "Yes";
      break;
    default:
      $prime = "No";
      break;
  }
?>
      <tr>
        <td><?php echo $i?></td>
        <td><?php echo $oddEven?></td>
        <td><?php echo $prime?></td>
      </tr>
<?php
}
?>
    </table>
  </body>
</html>

The code above generates the following result.