Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m Advertisement # Using the PHP Switch Statement Read Time:6 minsLanguages: 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. 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. 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. 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. If you’re thinking that it would only output Your favorite site is code.tutsplus.com!, you’re mistaken! It would output: 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.

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.

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.

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.