PHP Switch statement

The switch Statement

The switch statement is useful in cases in which one variable or the result of an expression can have multiple values, which should each trigger a different function.
For example, consider a PHP-driven menu system that passes a single string to the main menu code according to what the user requests. Let’s say the options are Home, About News, Login, and Links, and we set the variable $page to one of these, according to the user’s input.
If we write the code for this using if … elseif … else, it might look like Example 4-22.

Example 4-22. A multiple-line if … elseif … statement
<?php
if ($page == "Home") echo "You selected Home";
elseif ($page == "About") echo "You selected About";
elseif ($page == "News") echo "You selected News";
elseif ($page == "Login") echo "You selected Login";
elseif ($page == "Links") echo "You selected Links";
?>

If we use a switch statement, the code might look like Example 4-23.

Example 4-23. A switch statement
<?php
switch ($page)
{
case "Home":
echo "You selected Home";
break;
case "About":
echo "You selected About";
break;
case "News":
echo "You selected News";
break;
case "Login":
echo "You selected Login";
break;
case "Links":
echo "You selected Links";
break;
}
?>

As you can see, a $page is mentioned only once at the start of the switch statement.
Thereafter, the case command checks for matches. When one occurs, the matching
conditional statement is executed. Of course, in a real program, you would have a code here to display or jump to a page, rather than simply telling the user what was selected.

 

With switch statements, you do not use curly braces inside case
commands. Instead, they commence with a colon and end with the
break statement. The entire list of cases in the switch statement is
enclosed in a set of curly braces, though

Breaking out

If you wish to break out of the switch statement because a condition has been fulfilled, use the break command. This command tells PHP to break out of the switch and jump to the following statement. If you were to leave out the break commands in Example 4-23 and the case of Home evaluated to be TRUE, all five cases would then be executed. Or if a $page had the value News, then all the case commands from then on would execute. This is deliberate and allows for some advanced programming, but generally, you should always remember to issue a break command every time a set of case conditionals has finished executing. In fact, leaving out the break statement is a common error

Default action

A typical requirement in switch statements is to fall back on a default action if none of the case conditions is met. For example, in the case of the menu code in Example 4-23, you could add the code in Example 4-24 immediately before the final curly brace.

Example 4-24. A default statement to add to Example 4-23
default:
echo "Unrecognized selection";
break;

Although a break command is not required here because the default is the final substatement, and program flow will automatically continue to the closing curly brace, should you decide to place the default statement higher up it would definitely need a break command to prevent program flow from dropping into the following statements. Generally, the safest practice is to always include the break command.

Alternative syntax

If you prefer, you may replace the first curly brace in a switch statement with a single colon, and the final curly brace with an end switch command, as in Example 4-25.
However, this approach is not commonly used and is mentioned here only in case you
encounter it in third-party code.

Example 4-25. Alternate switch statement syntax
<?php
switch ($page):
case "Home":
echo "You selected Home";
break;
// etc...
case "Links":
 echo "You selected Links";
 break;
 endswitch;
?>

 

 

 

<< Prev Next >>

Loading