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 ($favorite_site == 'Design') { echo "Your favorite site is design.tutsplus.com!"; } elseif ($favorite_site == 'Code') { echo "Your favorite site is code.tutsplus.com!"; } elseif ($favorite_site == 'Business') { echo "Your favorite site is business.tutsplus.com!"; }
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 how the above example looks like with the switch statement.
switch ($favorite_site) { case 'Design': echo "Your favorite site is design.tutsplus.com!"; break; case 'Code': echo "Your favorite site is code.tutsplus.com!"; break; case 'Business': echo "Your favorite site is business.tutsplus.com!"; break; }
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.
switch (expression) { case value1: // code to be executed if expression equals to value1 break; case value2: // code to be executed if expression equals to value2 break; case value3: // code to be executed if expression equals to value3 break; ... ... default: // code to be executed if no cases are matched }
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.
<?php $favorite_site = 'Code'; switch ($favorite_site) { case 'Design': echo "Your favorite site is design.tutsplus.com!"; break; case 'Code': echo "Your favorite site is code.tutsplus.com!"; case 'Business': echo "Your favorite site is business.tutsplus.com!"; break; } ?>
If you’re thinking that it would only output Your favorite site is code.tutsplus.com!
, you’re mistaken! It would output:
Your favorite site is code.tutsplus.com! 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 about 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.
<?php switch ($favorite_site) { case 'Design': case 'Code': case 'Business': echo "It's great to see that you love one of our tutsplus group of websites!"; break; default: echo "You would like to explore tutsplus.com, we're sure you would love it!"; } ?>
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.
<?php switch (date("N", time())) { case 0: echo 'Sunday'; break; case 1: echo 'Monday'; break; case 2: echo 'Tuesday'; break; case 3: echo 'Wednesday'; break; case 4: echo 'Thursday'; break; case 5: echo 'Friday'; break; case 6: echo 'Saturday'; break; } ?>
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.
<?php switch( $type ) { case "Courses": switch( $category ) { case "Code": echo "Visit https://code.tutsplus.com/courses!"; break; case "Web Design": echo "Visit https://webdesign.tutsplus.com/courses!"; break; } break; case "Tutorials": switch( $category ) { case "Code": echo "Visit https://code.tutsplus.com/tutorials!"; break; case "Business": echo "Visit https://business.tutsplus.com/tutorials!"; break; } break; case "Guides": switch( $category ) { case "Code": echo "Visit https://code.tutsplus.com/series!"; break; case "Business": echo "Visit http://business.tutsplus.com/series!"; break; } break; } ?>
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.