PHP: Using switch() instead of if() + elseif()’s

PHP Friday, PHP+MySQL Add comments

It’s PHP Friday!
The PHP Friday Series

It makes for much cleaner code if you use the switch statement rather than if() followed by multiple elseif() statements. I have read many performance tests articles and switch() does process faster over if() + elseif()’s. But how does it look? How do you code it? Well below are some examples.

First, lets set a variable just to have it as an example:

1
$example_var = 7;

Now in most apps, you have most likely seen something similar to the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if ($example_var == 1) {
	// action 1 code here
}
else if ($example_var == 2) {
	// action 2 code here
}
else if ($example_var >= 3 && $example_var < = 6) {
	// action for 3 thru 6 code here
}
else if ($example_var == 7) {
	// action 7 code here
}
else {
	// for any other value do this
}

Those very same options can be coded into a more efficient switch() statement as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch($example_var) {
	case 1:
		// action 1 code here
		break;
	case 2:
		// action 2 code here
		break;
	case 3:
	case 4:
	case 5:
	case 6:
		// action for 3 thru 6 code here
		break;
	case 7:
		// action 7 code here
		break;
	default:
		// for any other value do this
		break;
}

or as another switch() statement alternative that gives you an interesting perspective or way of looking at it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch(true) {
	case ($example_var == 1):
		// action 1 code here
		break;
	case ($example_var == 2):
		// action 2 code here
		break;
	case ($example_var >= 3 && $example_var < = 6):
		// action for 3 thru 6 code here
		break;
	case ($example_var == 7):
		// action 7 code here
		break;
	default:
		// for any other value do this
		break;
}

As you can see by that last example, making the value in the switch() function to true or false and then loading switch cases that involve statements that are either true or not work just the same.

Summary:
So the variable enclosed in the switch() statement is what you are comparing its value to all the possible cases in the switch() statement that you have assigned. You assign each case a value or operation to see if its result matches the value of that variable. Everything located under that very case will be performed until it reaches the break statement letting it know to stop. Multiple cases can be brought together to all include the coding below it. It will keep performing all code below a matched case so long as a break statement is not met. default is the equivalent basically of just else. Its basically the “catch-all” and will always perform in the event that no other case above matched and had a break placed below it.

Definitely check it out over there on the wonderful packed full of information -> php manual:
PHP Manual: switch()

In the end, switch() may seem to be a little more difficult to code if you are not used to it, but if you look, it is a much easier code to read and know exactly what is going on. If the speed/benchmark tests that I have read about are true, than of course you want to use switch() as your primary function for this sort of coding anyway.

Did you like this post? I write many like it all the time!
Please Subscribe To My RSS Feed!
To catch many more articles like this in the future, make it easy on yourself and subscribe to me via RSS. You will not regret it!

Add This Site To Your Technorati Favorites!
Let me know that you added me by posting a comment below and then I will add you as a favorite of mine as well!

Please Give Me Some Rice!
Giving me "rice" on Nice4Rice.com also benefits you by giving you a free linkback on that site too.
Here are some Related Posts:

RSS feed | Trackback URI

3 Comments »

Comment by Saman Sadeghi UNITED STATES Windows XP Mozilla Firefox 2.0.0.4
2007-06-25 22:48:49

A very interesting article indeed! I have always wondered how inefficient IF statements can be, nice to see that there is an alternative!

PHP is truly amazing!

Oh, and it’s Monday :razz:

Comment by RJ Matthis UNITED STATES Windows XP Mozilla Firefox 2.0.0.4
2007-06-26 22:44:10

Yes it was Monday yesterday…but I did post it last Friday. Did your RSS Reader not receive it till Monday?

Comment by Saman Sadeghi UNITED STATES Windows XP Mozilla Firefox 2.0.0.4
2007-06-29 14:28:24

Nope, it gave the post date of Monday! :shock:

 
 
 

Leave a Reply

Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.
"Reformatted", but based on a WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login
Close
E-mail It