Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Switch and IF?

Tags:

php

I know this may be simple question but want to know every ones opinion on this.

what is the difference between switch and IF function in PHP?? What I can see is where ever "switch" function uses "IF" function also applies there..correct me if I am wrong..

Or any performance wise difference between two??

like image 712
mathew Avatar asked Jul 05 '10 10:07

mathew


People also ask

What is better a switch or if?

A switch statement is usually more efficient than a set of nested ifs. Deciding whether to use if-then-else statements or a switch statement is based on readability and the expression that the statement is testing.


3 Answers

Or any performance wise difference between two??

Forget about the performance difference on this level- there may be a microscopic one, but you'll feel it only when doing hundreds of thousands of operations, if at all. switch is a construct for better code readability and maintainability:

switch ($value) 
 {
 case 1:   .... break;
 case 2:   .... break;
 case 3:   .... break;
 case 4:   .... break;
 case 5:   .... break;
 default:  .... break;
 }

is mostly much more clean and readable than

if ($value == 1) { .... }
elseif ($value == 2) { .... }
elseif ($value == 3) { .... }
elseif ($value == 4) { .... }
elseif ($value == 5) { .... }
else { .... }

Edit: Inspired by Kuchen's comment, for completeness' sake some benchmarks (results will vary, it's a live one). Keep in mind that these are tests that run 1,000 times. The difference for a couple of if's is totally negligeable.

  • if and elseif (using ==) 174 µs
  • if, elseif and else (using ==) 223 µs
  • if, elseif and else (using ===) 130 µs
  • switch / case 183 µs
  • switch / case / default 215 µs

Conclusion (from phpbench.com):

Using a switch/case or if/elseif is almost the same. Note that the test is unsing === (is exactly equal to) and is slightly faster then using == (is equal to).

like image 86
Pekka Avatar answered Oct 04 '22 07:10

Pekka


If you have simple conditions, like if something equates to something else, then a switch is ideal.

For example, instead of doing the following:

if($bla == 1) {

} elseif($bla == 2) {

} elseif($bla == 3) {

} etc...

It's better to do it like this:

switch($bla) {
  case 1:
    ...
    break;
  case 2:
    ...
    break;
  case 3:
    ...
    break;
  default:
    ...
    break;
}

Alternatively, if you have complex conditions, you should use an if/else.

I think that this is all a matter of opinion though - some people just don't use switch statements at all, and stick with if/else.

like image 38
xil3 Avatar answered Oct 04 '22 09:10

xil3


No, you are right.
There are not much difference between these statements.
You may use one you like.

Just bear in mind that if you have to use more than 3-4 consecutive conditions - that means you most likely have design faults.

Usually you can substitute such a statement with a loop or with more clear application design.

like image 30
Your Common Sense Avatar answered Oct 04 '22 07:10

Your Common Sense