# Using the PHP Switch Statement

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:

#### 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.