PHP Basics: The Switch Statement

/ PHP / by Paul Robinson / 2 Comments
This post was published back on May 20, 2010 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

A PHP switch statement is basically an if…else statement and is probably best used when the latter would become too large.

Basic Switch Statement

Let’s take a look at a basic switch statement:

At first it may look confusing, but let’s see if we can change that. First let’s look at a description of how a switch statement works. Don’t worry if you don’t really understand it as I’m going to simplify it a little later.

You provide a value, normally a variable. The value is then used to evaluate each case. If the case equates to true then the case is executed, if a break is encountered the switch stops. If a break is not encountered each case beneath the one just executed will also be executed until the end of the switch is reached or a break is encountered. If no case equates to true then the default case will be executed.

That may all sound a little confusing too, but don’t worry we are going to go through it in some more detail now. First you provide something for the switch to check. This can be a variable, or an expression of some sort. The value, of said variable/expression, is then used to check against each case. If the case matches the value the code for that case is executed.

Switch Fallthrough

One of the sometimes useful, and quite quirky features of a switch is that unless a break is found after a case it will continue to run each case below until it reaches the end of the switch, or a break is found. Let’s see what that would look like:

In this case the result would be:

one two three

As you can see although the variable $i only matched the first case, because no break was found it ran through until it found the break on the third case.

Switch Default Case

A very important feature in a switch statement is the default case. It does exactly what you would think. If no cases are matched then the default is ran. Here is an example:

Since $i is a value not listed in any case the default case will be ran instead.

Conditions As Cases

A very useful, but possibly little known, feature of the switch statement is the ability to use conditions in place of a case. Let’s look at one:

As you can see this is similar to how you might normally use an if…else statement. The benefit is that switch statements can have a lot of cases, will be easier to read, and have (arguable) speed benefits over large if…else statements.

There we have a quick overview of PHP’s switch statement. Hopefully you’ve learned something new or even just brushed up on somethings you’d forgotten. Whatever the case I hope you found this post useful. As always if you have a question, comment or something to add leave it in the comments, I always love to hear from you.

2 Comments

Author’s gravatar

Please please please don’t ever use the last one. It’s a maintenance nightmare, which after all is what code is about now.

Reply
Author’s gravatar author

I was always taught that too, but as this was a look at the uses for switch statements I didn’t want to leave it out.

I’ve read a few reasons advising against using that sort of switch, would you mind letting me know exactly why you think it shouldn’t be used. I might then add it to the post. 😉

Older Comments
Newer Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more