PHP Tutorials

Tutorial 6 - PHP Switch Statements

In Tutorial 5 we took a look at if statements for controlling conditional execution of code. An alternative to this is the switch statement. Switch statements are useful if you want to test a single variable for several different values and perform a different operation for each value.

Anything that can be done with a switch can also be done using if statements (but not the other way round). Sometimes a switch looks better and gives someone reading your code a clue about what you are trying to do.

The following example shows how we might code a test using the if statements we learnt about in Tutorial 5. It takes the position that we finished a race and displays whether it is First, Second or Third.


<?php

// $position contains our position in a race

if( $position == )
{
    echo 
"First\n";
}
else if ( 
$position == )
{
    echo 
"Second\n";
}
else if ( 
$position == )
{
    echo 
"Third\n";
}

?>

We can write exactly the same thing using a switch statement. Each possible position becomes a "case" in our switch statement:


<?php

switch ( $position )
{
    case 
1:
        echo 
"First\n";
        break;
    case 
2:
        echo 
"Second\n";
        break;
    case 
3:
        echo 
"Third\n";
        break;
}

?>

Notice the "break" statements. These tell PHP that you have finished that "case". If we leave out the "break" we will "fall through" to the case below. We can illustrate this with an example that gives a star rating, depending on our race position (3 stars for first place, 2 stars for second and 1 star for third):


<?php

switch ( $position )
{
    case 
1:
        echo 
"*";
        
// Fall through
    
case 2:
        echo 
"*";
        
// Fall through
    
case 3:
        echo 
"*";
}

?>

We can use fall through to execute the same code for different conditions. For example:


<?php

switch ( $fruit )
{
    case 
'apple':
    case 
'banana':
        echo 
"Yum yum\n";
        break;

    case 
'pear':
        echo 
"Not bad\n";
        break;

    case 
'lemon':
        echo 
"Yuck\n";
        break;
}

?>

In the code above, if $fruit is either apple or banana we display "Yum yum". The apple case falls through to the banana case.

What about if none of the cases are executed? Well - we can use the "default" statement to do something like this:


<?php

switch ( $fruit )
{
    case 
'apple':
    case 
'banana':
        echo 
"Yum yum\n";
        break;

    case 
'pear':
        echo 
"Not bad\n";
        break;

    case 
'lemon':
        echo 
"Yuck\n";
        break;

    default:
        echo 
"I've never tried them before\n";
        break;
}

?>

If it's not apple, banana, pear, or lemon, then the message "I've never tried them before" will be displayed.

Switches can be useful to execute different pieces of code, depending on the value of 1 variable. Using a switch instead of ifs can make your code easier to read since the reader knows what is being tested without having to read all of the if conditions.