TOC

The community is working on translating this tutorial into French, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Basic PHP:

The switch statement

You can consider the switch statement as an alternative to several if statements, a control structure with a range of options. For each option, you can define an action. Switch statements are great for choices with many options, because they are easy to overview and modify to reflect changes in your situation.

The switch statement is built up around a condition, and then a number of case statements which can each lead to an action. Here' s an example:

<?php
$answer = 0;
if(isset($_GET["answer"]))
    $answer = $_GET["answer"];
    
switch($answer)
{
    default:
        echo "Which version of PHP are you using?<br /><br />";
        echo "<a href=\"?answer=3\">3</a><br />";
        echo "<a href=\"?answer=4\">4</a><br />";
        echo "<a href=\"?answer=5\">5</a><br />";
        break;
    case 3:
        echo "Ugh that's old, upgrade now!";
        break;
    case 4:
        echo "Still on version 4? Give PHP 5 a try!";
        break;
    case 5:
        echo "Good choice!";
        break;
}
?>

The first couple of lines are used to retrieve an answer to our question from the query string - don't worry too much about that right now, the $_GET and $_POST variables will be explained later on.

Then we get to the actual switch statement. It consists of the switch keyword and then the condition that we want to evaluate. In our case, we use the $answer variable, which will hold a value passed through the query string. Inside the switch statement, we start defining our case's. The first case is a bit special, because instead of looking for a single value, we use the default keyword to define an action that will happen only if none of the other cases matches. In our example, this allows us to output the question that we want to ask and then some links for answering it. We ask the user which version of PHP they are using, and as soon as they click one of the answer links, we will output an answer for them. If no answer is selected, or if an unknown answer is entered through the query string, we simply output the question, since it's our default case.

Each case is built declared by using the case keyword, the value we wish to check for, a colon, the code we wish to execute if we have the right case, and then the break statement to signal that if a case statement matches, we want the switch statement to end.

This might seem a bit complicated, but try running the example and try the different options and you will soon see that switch statements are pretty easy to work with.

Switch statements allows you to group several options into one case, and in PHP, you can test against strings as well. Here's a small example:

$color = "red";
switch($color)
{
    case "red":
    case "blue":
    case "green":
        echo "Nice basic color!";
        break;
    case "black":
        echo "Too dark!";
        break;
    case "white":
        echo "Too bright!";
        break;
}

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!