PHP Basics: The Switch Statement
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$i = 1; switch($i) { case 0 : echo 'This is zero'; break; case 1 : echo 'This is one'; break; case 2 : echo 'This is two'; break; default : echo 'This is NaN'; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$i = 1; switch($i) { case 1 : echo 'one '; case 2 : echo 'two '; case 3 : echo 'three '; break; case 4 : echo 'four '; break; } |
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:
1 2 3 4 5 6 7 8 |
$i = 9; switch($i) { case 1 : echo 'one'; break; default : echo 'Ooops!'; } |
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:
1 2 3 4 5 6 7 8 |
$i = 2; switch(TRUE) { case ($i % 2 == 0) : echo 'The number is a multiple of 2'; break; default : echo 'The number is not a multiple of 2'; } |
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
Ross
Please please please don’t ever use the last one. It’s a maintenance nightmare, which after all is what code is about now.
Paul Robinson
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. 😉