1. Code
  2. Coding Fundamentals

Using the PHP Switch Statement

Scroll to top

In this article, we’ll discuss the basics of the switch statement in PHP. Along with the syntax of the switch statement, we’ll also go through a couple of real-world examples to demonstrate how you can use it.

What Is the Use of the Switch Statement?

In PHP, the switch statement is considered as an alternative to if-elseif-else statements. More often than not, you want to compare a variable with different values, and based on that you want to execute a piece of code. The first thing which comes to your mind is to implement a series of if-elseif-else statements to achieve it, as shown in the following snippet.

1
if ($favorite_site == 'Design') {
2
    echo "Your favorite site is design.tutsplus.com!";
3
} elseif ($favorite_site == 'Code') {
4
    echo "Your favorite site is code.tutsplus.com!";
5
} elseif ($favorite_site == 'Business') {
6
    echo "Your favorite site is business.tutsplus.com!";
7
}

If it’s just a matter of two or three conditions, it’s fine to implement it with the if-elseif-else statements. But if you need to deal with several conditions, and if you implement it with the if-elseif-else statements, the code becomes really lengthy, and it’s difficult to manage it. In such cases, it’s ideal to use the switch statement, and that’s exactly what the switch statement is designed for.

Before we go ahead and discuss the switch statement in detail, let’s quickly see what the above example looks like with the switch statement.

1
switch ($favorite_site) {
2
    case 'Design':
3
        echo "Your favorite site is design.tutsplus.com!";
4
        break;
5
    case 'Code':
6
        echo "Your favorite site is code.tutsplus.com!";
7
        break;
8
    case 'Business':
9
        echo "Your favorite site is business.tutsplus.com!";
10
        break;
11
}

Doesn't that look much more compact and neat? So the switch statement comes in really handy when it comes to dealing with multiple conditions in your code.

Syntax and Examples

In this section, we’ll go through the syntax of the switch statement and a couple of real-world examples to demonstrate how you can use it in your day-to-day PHP development.

Syntax

Let’s go through the syntax of the switch statement.

1
switch (expression)
2
{
3
  case value1:
4
    // code to be executed if expression equals to value1

5
    break;
6
  case value2:
7
    // code to be executed if expression equals to value2

8
    break;
9
  case value3:
10
    // code to be executed if expression equals to value3

11
    break;
12
    ...
13
    ...
14
  default:
15
    // code to be executed if no cases are matched

16
}

It starts with the expression in the switch statement. Firstly, the expression is evaluated, and the outcome of the expression is compared with all the cases in sequence until there’s a match. If there’s a match, the code associated with that particular case is executed. If there’s no match, and there's a default case available, it will execute that code.

It’s important that you must not forget the break statement in each case. If a case is matched, and there’s no break statement in that case, the code of all the following cases will be executed, even though the case values don’t match.

Let’s go through the following example to understand the importance of the break statement.

1
<?php
2
$favorite_site = 'Code';
3
switch ($favorite_site) {
4
    case 'Design':
5
        echo "Your favorite site is design.tutsplus.com!";
6
        break;
7
    case 'Code':
8
        echo "Your favorite site is code.tutsplus.com!";
9
    case 'Business':
10
        echo "Your favorite site is business.tutsplus.com!";
11
        break;
12
}
13
?>

If you’re thinking that it would only output Your favorite site is code.tutsplus.com!, you’re mistaken! It would output:

1
Your favorite site is code.tutsplus.com!
2
Your favorite site is business.tutsplus.com!

This is because you haven’t included the break statement in the second case, and thus PHP would execute the code of all the following cases, irrespective of the value of the $favorite_site variable. So you should never forget the break statement when you're using the switch statement in your code.

Finally, the default case is optional; it’s not mandatory to provide it. It serves the purpose of providing the default code which is executed if the expression value doesn’t match with any cases.

So that’s the syntax of the switch statement. We’ll see a couple of real-world examples in the next section.

Examples

Now, you’re familiar with the syntax of the switch statement, and you’ve already gone through a couple of basic examples in the previous section. In this section, we’ll go through a couple of advanced examples of the switch statement.

Multiple Cases for the Same Code-Block

Let’s go through the following example.

1
<?php
2
switch ($favorite_site)
3
{
4
    case 'Design':
5
    case 'Code':
6
    case 'Business':
7
            echo "It's great to see that you love one of our tutsplus group of websites!";
8
            break;
9
    default:
10
            echo "You would like to explore tutsplus.com, we're sure you would love it!";
11
}
12
?>

Sometimes you want to execute the same piece of code for different cases. The above example demonstrates it. In the above example, if the value of the $favorite_site variable is 'Design' or 'Code' or 'Business', it would execute the same code for all these three cases. Otherwise, it would execute the code associated with the default case.

Expression in Switch Statements

In the examples that we’ve discussed so far, we’ve used a variable in the switch statement. In fact, the switch statement allows you to use an expression as well.

Let’s see how you can use an expression in the switch statement, as shown in the following example.

1
<?php
2
switch (date("N", time()))
3
{
4
    case 0:
5
        echo 'Sunday';
6
        break;
7
    case 1:
8
        echo 'Monday';
9
        break;
10
    case 2:
11
        echo 'Tuesday';
12
        break;
13
    case 3:
14
        echo 'Wednesday';
15
        break;
16
    case 4:
17
        echo 'Thursday';
18
        break;
19
    case 5:
20
        echo 'Friday';
21
        break;
22
    case 6:
23
        echo 'Saturday';
24
        break;
25
}
26
?>

First, PHP evaluates the expression in the switch statement, and the outcome of that expression is matched with all the cases. In this case, the statement outputs the current day of the week.

Nested Switch Statements

Finally, you can also nest the switch statements. Let’s see how this works with the following example.

1
<?php
2
switch( $type )
3
{
4
    case "Courses":
5
        switch( $category )
6
        {
7
            case "Code":
8
                   echo "Visit https://code.tutsplus.com/courses!";
9
                break;
10
            case "Web Design":
11
                   echo "Visit https://webdesign.tutsplus.com/courses!";
12
                break;
13
        }
14
        break;
15
    case "Tutorials":
16
        switch( $category )
17
        {
18
            case "Code":
19
                echo "Visit https://code.tutsplus.com/tutorials!";
20
                break;
21
            case "Business":
22
                echo "Visit https://business.tutsplus.com/tutorials!";
23
                break;
24
        }
25
        break;
26
    case "Guides":
27
        switch( $category )
28
        {
29
            case "Code":
30
                echo "Visit https://code.tutsplus.com/series!";
31
                break;
32
            case "Business":
33
                echo "Visit http://business.tutsplus.com/series!";
34
                break;
35
        }
36
        break;  
37
}
38
?>

As you can see, it’s perfectly valid to use a switch statement inside another switch statement. However, you should try to avoid this, since it makes your code harder to read and maintain.

Conclusion

Today, we discussed how you can use the switch statement in PHP. It's a really useful language feature that allows you to simplify lengthy if-elseif-else statements and keeps your code clear and readable.

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.