eHow launches Android app: Get the best of eHow on the go.

How To

How to Create a Switch Statement in PHP

Contributor
By eHow Contributing Writer
(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.

Difficulty: Moderately Easy
Instructions

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

  1. Step 1

    Write the word "switch," followed by the name of the variable you need to check. Put the variable name in brackets:
    switch ($title)

  2. Step 2

    Enter {}.

  3. Step 3

    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;

  4. Step 4

    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;

  5. Step 5

    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).

  6. Step 6

    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?";

  7. Check the Statement

  8. Step 1

    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?";
    }

  9. Step 2

    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.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Internet Newsletters

Copyright © 1999-2010 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

Demand Media
eHow_eHow Technology and Electronics