The Switch Structure

Switch cases

How they work

Here's an example of a simple game where a user enters a $user_command and different functions are run as a result:

if ($user_command == "n") {
  go_north;
} else if ($user_command == "e") {
  go_east;
} else if ($user_command == "s") {
  go_south;
} else if ($user_command == "w") {
  go_west;
} else {
  do_something_else;
}

Clearly, there's a lot of repeated code here. The switch case structure allows you to avoid this redundant code. It allows programmers to repeatedly compare the value of a certain variable to a list of possible values and execute code based on the result. This is the syntax for a switch case statement, compared to the same code written using if statements:

if statement style switch case style
if ($firstvariable == 'comparison1' 
    || $firstvariable == 'comparison2') {

  doSomething;
  doSomethingElse;
} else if ($firstvariable == 'comparison3') {
  doAThirdThing;
} else {
  launchMissiles;
}
// Look at how much switch case saves you!
switch($firstvariable) {
  case 'comparison1':
  case 'comparison2':
    doSomething;
    doSomethingElse;
    break;
  
  case 'comparison3':
    doAThirdThing;
    break;
  
  default: 
    launchMissiles;
    break;
}

The switch case style will save you from retyping $firstvariable, and make your code look cleaner (especially, if that code is a long chain of simple if statements). Returning to our zorkmid sample program, we have:

Original Code Switch-Case Code
if ($user_command == "n") {
  go_north;
} else if ($user_command == "e") {
  go_east;
} else if ($user_command == "s") {
  go_south;
} else if ($user_command == "w") {
  go_west;
} else {
  do_something_else;
}
switch($user_command) {
  case 'n':
    go_north;
    break;
  case 'e':
    go_east;
    break;
  case 's':
    go_south;
    break;
  case 'w':
    go_west;
    break;
  default:
    do_something_else;
    break;
}

Syntax

switch($var) {
  case [value]:
    [code]
    break;
  
  case [value]:
    [code]
    break;
  
  ...
  
  default:
    [code]
    break;
}

In this example, $var is the first variable to be compared. This variable is then compared against each case statement from the top down, until it finds a match. At that point, the code will excecute until a break statement is reached (that will allow you to leave the case statement entirely).

Important warning about using switch case statements

Don't forget to use break when you mean break! If you forget, you might run functions you don't intend to. However, there are circumstances where leaving breaks out can be useful. Consider this example:

switch ($n) {
  case 0:
  case 1:
  case 2:
    //only executes, if $n is 0, 1 or 2
    doSomethingForNumbers2OrSmaller;
    break;
  case 3:
    //only executes, if $n is 3
    doSomethingForNumber3;
  default:
    //only executes, if $n is 3 or above
    doSomethingForNumbers3OrBigger;
    break;
}

This kind of coding is sometimes frowned upon, since it's not always as clear to see what the code is meant to do. Also, consider commenting case statements that aren't supposed to have a break; statement before the next case, so when others look at your code, they know not to add a break. In such a case, it is good programming practice to add comments to breakless cases so that it is clear that the break has been omitted deliberately:

switch ($n) {
  case 0:
    // Falls through!
  case 1:
    doSomethingForLargeNumbers;
    // Falls through!
  case 2:
    doSomethingForSmallerNumbers;
    break;
  ...
}

For more information



  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.

PHP_Programming/The_switch_Structure
 



 

Connect with defaultLogic
What We've Done
Led Digital Marketing Efforts of Top 500 e-Retailers.
Worked with Top Brands at Leading Agencies.
Successfully Managed Over $50 million in Digital Ad Spend.
Developed Strategies and Processes that Enabled Brands to Grow During an Economic Downturn.
Taught Advanced Internet Marketing Strategies at the graduate level.


Manage research, learning and skills at defaultLogic. Create an account using LinkedIn or facebook to manage and organize your Digital Marketing and Technology knowledge. defaultLogic works like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us