Things You'll Need:
- PHP 5, installed and properly configured
- MySQL database server, configured for work with PHP
- Web server
- PHP IDE
-
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) -
Step 2
Enter {}.
-
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; -
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; -
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).
-
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?"; -
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?";
} -
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.












