Thursday 26 July 2012

PHP Switch Statement - Syntax - Multiple case - Default Case - Example




PHP Switch Statement



In the previous lessons we covered the various elements that make up an If Statement in PHP. However, there are times when an if statement is not the most efficient way to check for certain conditions.

For example we might have a variable that stores travel destinations and you want to pack according to this destination variable. In this example you might have 20 different locations that you would have to check with a nasty long block of If/ElseIf/ElseIf/ElseIf/... statements. This doesn't sound like much fun to code, let's see if we can do something different.

PHP Switch Statement: Speedy Checking

With the use of the switch statement you can check for all these conditions at once, and the great thing is that it is actually more efficient programming to do this. A true win-win situation!
The way the Switch statement works is it takes a single variable as input and then checks it against all the different cases you set up for that switch statement. Instead of having to check that variable one at a time, as it goes through a bunch of If Statements, the Switch statement only has to check one time.

PHP Switch Statement Example

In our example the single variable will be $destination and the cases will be: Las Vegas, Amsterdam, Egypt, Tokyo, and the Caribbean Islands.

PHP Code:

$destination = "Tokyo";
echo "Traveling to $destination<br />";
switch ($destination){
 case "Las Vegas":
  echo "Bring an extra $500";
  break;
 case "Amsterdam":
  echo "Bring an open mind";
  break; 
 case "Egypt":
  echo "Bring 15 bottles of SPF 50 Sunscreen";
  break; 
 case "Tokyo":
  echo "Bring lots of money";
  break;
 case "Caribbean Islands":
  echo "Bring a swimsuit";
  break; 
}

Display:

Traveling to Tokyo
Bring lots of money 

Default Case

You may have noticed the lack of a place for code when the variable doesn't match our condition. The if statement has the else clause and the switch statement has the default case.
It's usually a good idea to always include the default case in all your switch statements. Below is a variation of our example that will result in none of the cases being used causing our switch statement to fall back and use the default case. Note: the word case does not appear before the word default, as default is a special keyword!

PHP Code:

$destination = "New York";
echo "Traveling to $destination<br />";
switch ($destination){
 case "Las Vegas":
  echo "Bring an extra $500";
  break;
 case "Amsterdam":
  echo "Bring an open mind";
  break;
 case "Egypt":
  echo "Bring 15 bottles of SPF 50 Sunscreen";
  break; 
 case "Tokyo":
  echo "Bring lots of money";
  break; 
 case "Caribbean Islands":
  echo "Bring a swimsuit";
  break;  
 default:
  echo "Bring lots of underwear!";
  break;
}

Display:

Traveling to New York
Bring lots of underwear!




0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More