1. Code
  2. Coding Fundamentals

PHP Case Statement Quick Reference

Scroll to top
5 min read

Have you ever wanted to compare the value of a variable or expression to multiple possible results and conditionally execute some code? There are many options available for you to do that. You could either use multiple if-else conditions or you could use switch-case statements.

In this tutorial, I will show you how to use switch-case in PHP to compare a variable with many different values.

Using switch-case vs. if-else: Direct Comparison

Let's begin with a simple example that shows a direct comparison between using if-else and switch-case to control program flow.

1
<?php
2
3
$animals = ['Elephant', 'Lion', 'Deer', 'Goat', 'Crocodile', 'Zebra', 'Horse'];
4
5
shuffle($animals);
6
7
$animal = $animals[0];
8
9
// Outputs: Lions are in the south-west section of the zoo.

10
if($animal == 'Elephant') {
11
    echo 'Elephants are in the south section of the zoo.';
12
13
} elseif($animal == 'Lion') {
14
    echo 'Lions are in the south-west section of the zoo.';
15
16
} elseif($animal == 'Deer') {
17
    echo 'Deer are in the north section of the zoo.';
18
19
} elseif($animal == 'Goat') {
20
    echo 'Goats are in the east section of the zoo.';
21
} else {
22
    echo 'Ask a tour guide.';
23
}
24
25
// Outputs: Lions are in the south-west section of the zoo.

26
switch($animal) {
27
    case 'Elephant':
28
        echo 'Elephants are in the south section of the zoo.';
29
        break;
30
    case 'Lion':
31
        echo 'Lions are in the south-west section of the zoo.';
32
        break;
33
    case 'Deer':
34
        echo 'Deer are in the north section of the zoo.';
35
        break;
36
    case 'Goat':
37
        echo 'Goats are in the east section of the zoo.';
38
        break;
39
    default:
40
        echo 'Ask a tour guide.';
41
}
42
?>

Try running the above code multiple times to get different output. You will notice a few things.

Each case statement is similar to an if or elseif block. The output you get from the if-else section is the same as the output from the switch-case section. Always remember to use a break statement to stop the execution of code for each case statement.

Let's say you forgot to include break with each case block and the value of $animal is 'Lion'. In that case, the code will output the location of lions, deer, and goats, as well as the default statement about the tour guide.

The default statement is like a catch-all that handles all other values of $animal. It is similar to the else block that we use in the if-else section. Remember that you cannot use multiple default statements in your code.

Checking Multiple Conditions at Once

What do you do if there are multiple animals in the same section of the zoo? With if-else blocks, you can put them all inside one conditional statement using || to see if any of them evaluates to true. Here is an example:

1
$animal = 'Zebra';
2
3
// Outputs: Zebras are in the west section of the zoo.

4
if($animal == 'Elephant' || $animal == 'Lion' || $animal == 'Goat') {
5
    echo $animal.'s are in the south section of the zoo.';
6
} elseif($animal == 'Zebra') {
7
    echo 'Zebras are in the west section of the zoo.';
8
} elseif($animal == 'Deer') {
9
    echo 'Deer are in the east section of the zoo.';
10
} else {
11
    echo 'Ask a tour guide.';
12
}

The code above would correctly tell us that Zebras are in the west section of the zoo. It skipped the code inside the first if block because that is only executed when the $animal is an elephant, lion, or goat.

Let's rewrite the above code in the form of switch-case statements. You might be tempted to write it as shown below:

1
// Outputs: Zebras are in the east section of the zoo.

2
switch($animal) {
3
    case 'Elephant' || 'Lion' || 'Goat':
4
        echo $animal.'s are in the east section of the zoo.';
5
        break;
6
    case 'Zebra':
7
        echo 'Zebras are in the west section of the zoo.';
8
        break;
9
    case 'Deer':
10
        echo 'Deer are in the north section of the zoo.';
11
        break;
12
    default:
13
        echo 'Ask a tour guide.';
14
}

Look closely and you will see that the statement now says zebras are in the east section of the zoo. What happened here?

The value or expression that we pass to switch is loosely compared to the value or expression that we pass with each case statement. Now, 'Elephant' || 'Lion || 'Goat' ultimately evaluates to true. This also returns true when loosely compared to the value inside $animal because a non-empty string is a truthy value. The code for the first case statement is then executed, and we get the wrong location for the same zoo.

The correct way to handle multiple case statements which are supposed to execute the same section of code is by writing the case values on separate lines, as shown below:

1
// Outputs: Zebras are in the west section of the zoo.

2
switch($animal) {
3
    case 'Elephant':
4
    case 'Lion':
5
    case 'Goat':
6
        echo $animal.'s are in the east section of the zoo.';
7
        break;
8
    case 'Zebra':
9
        echo 'Zebras are in the west section of the zoo.';
10
        break;
11
    case 'Deer':
12
        echo 'Deer are in the north section of the zoo.';
13
        break;
14
    default:
15
        echo 'Ask a tour guide.';
16
}

We get the correct location of zebras now, and no visitor will get lost when looking for them.

Repeat Evaluation and Performance

One more thing that you should keep in mind is that the condition is evaluated only once when using a switch statement. On the other hand, it is evaluated multiple times when using an if-else statement, i.e. once for each if block. Here's an example:

1
function most_populated($country) {
2
    return 'New York';
3
}
4
5
// Outputs: This is not surprising at all.

6
switch(most_populated('United States')) {
7
    case 'New York':
8
        echo 'This is not surprising at all.';
9
        break;
10
    case 'Oakland':
11
        echo 'This does not seem correct.';
12
        break;
13
}

We make a call to the most_populated() function, which returns the name of a city. It simply returns New York in this case, but it could easily be much more complicated and get city data from a web service for the passed country. Writing multiple if-else conditions in this case would have resulted in multiple calls to the most_populated() function.

Obviously, we could avoid all those calls by storing the call result in a variable, but using switch allows you not to use any variables at all.

Final Thoughts

In this tutorial, I showed you how to use switch-case statements in PHP in place of if-else blocks to execute code conditionally. We also saw how to write case statements that cover multiple conditions. Finally, we learned how using switch-case can be faster than if-else blocks in certain situations.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.