How to Create a Switch Statement in PHP

By eHow Internet Editor

Rate: (2 Ratings)

Conditional statements are one of the most powerful tools in any programming language because, without them, it would not be possible to control the program flow. Like any other programming language, PHP has a set of conditional statements that allow your code to perform a different action when a condition is or is not met. One of these statements is the switch statement.

Instructions

Difficulty: Moderately Easy

Things You’ll Need:

  • PHP 5, installed and properly configured
  • MySQL database server, configured for work with PHP
  • Web server
  • PHP IDE

Create a Switch Statement in PHP

Step1
Write the word "switch," followed by the name of the variable you need to check. Put the variable name in brackets:
switch ($title)
Step2
Enter {}.
Step3
Enter the first conditional block. Pay attention to the fact that each case statement ends with a colon (:) and the rest end with a semi-colon(;):
case "E.T":
case "Star Wars":
echo "Sorry, $title is already taken! Please check later";
break;
Step4
Enter the second conditional block, which is executed if the statement in the first block is false:
case "Casablanca":
case "Breakfast at Tiffany's":
echo "Sorry, we don't lend $title.";
break;
Step5
Enter as many other case blocks as needed. If you don't need any other case blocks, enter the block of code that will be executed by default when all the preceding blocks are false. In this example, the default block will be to offer the title for lending, since it is not already taken (as checked by the first statement) and is not unavailable for lending (as with the titles in the second statement).
Step6
Use the following block to notify the customer if the title is available and can be lent:
default:
echo "$title is available. Would you like to book it?";

Check the Statement

Step1
See how the whole piece of code looks when assembled together:
switch ($title) {
case "E.T":
case "Star Wars":
echo "Sorry, $title is already taken! Please check later";
break;
case "Casablanca":
case "Breakfast at Tiffany's":
echo "Sorry, we don't lend $title.";
break;
default:
echo "$title is available. Would you like to book it?";
}
Step2
Check to see that you have not omitted a break, brace, semicolons or colons, because these syntax errors will prevent the code from executing properly.

Tips & Warnings

  • The default clause is optional, but it is highly recommended. You can't always predict what the program will do if none of the conditions are met.
  • Pay attention to the syntax of the switch statement that you create. This is especially important if you have been programming in other languages and now you have to switch to PHP for a project or two.
  • The switch statement is enclosed in {}, so do not forget the closing } when you create your code.
  • Do not forget the break statement. If you omit the break statement when a case statement has been matched, the rest of the PHP code that follows will be executed even if there is another case statement checking for a different value. It is true that this behavior can be useful, but unless you intend to continue the execution of the rest of the code, put a break after every case.

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article: How to Create a Switch Statement in PHP

eHow Internet Editor

eHow Internet Editor

Category: Internet

Articles: See my other articles

Related Ads

Internet

Veesites
Meet Virginia DeBolt eHow’s Internet Expert.