5

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??

mathew
  • 1,179
  • 2
  • 13
  • 24

5 Answers5

19

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).

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • especially when there are more than 3 options.. +1 – Sergey Eremin Jul 05 '10 at 10:44
  • completely agree - far too much emphasis on micro-optimisations that always seem to sacrifice readability. Personally, for anything beyond a single if/else i always use switch statements – robjmills Jul 05 '10 at 10:45
  • 2
    for the sake of completeness, if-else is a bit faster than switch. But again, at this level (especially with PHP) readability > performance. – Kuchen Jul 05 '10 at 10:53
  • @Kuchen good point! I added some benchmarks from phpbench.com. – Pekka Jul 05 '10 at 10:58
  • 1
    @Kuchen awful point. that's not the difference to OP asked about. It does not worth mentioning because will mislead all these ignorant monkeys hoping about. PHP already suffer from terrible performance-wise misbeliefs and you just added to that. These stupid tests that tests nothing are much worst than if there was no test at all – Your Common Sense Jul 05 '10 at 11:02
  • @Col bullshit. The OP asked about performance differences, and it is perfectly fine to answer that. There are warnings about how unimportant and negligeable the differences are all over the place now, including @Kuchen's comment. If somebody chooses to ignore those, there's nothing we can do. I'm grateful for those tests because they help *dissolve* performance myths. – Pekka Jul 05 '10 at 11:06
  • @Col if what Kuchen said was not what the OP meant in your opinion, what *did* he mean? I don't understand. – Pekka Jul 05 '10 at 11:11
  • Forget it. One who doing such tests doesn't deserve any argument. phpbench author should be executed for youth's minds corruption. – Your Common Sense Jul 05 '10 at 11:27
  • He probably just wanted a more detailed "whats the difference" (switch -> loose comparison // if -> can be strict ... something like that i guess (yes, case (x === y): bleh..)) but he did mention performance in the question and its a fact that if-else is a tick faster. All you can do is explain the "ignorant monkeys" why its better to go switch now and then.. – Kuchen Jul 05 '10 at 11:42
  • If anyone still reads this... I was just looking for general switch vs multiple ifs... I read this and very interesting +1... however, I am not a PHP guy and maybe this is an easy one... What is the difference between == and === ... I don't know difference between equal and exactly equal! Thanks! – Wil Mar 12 '11 at 07:03
  • 1
    @Wil http://stackoverflow.com/questions/1139154/is-there-a-difference-between-and-in-php – Pekka Mar 12 '11 at 09:46
3

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.

xil3
  • 16,305
  • 8
  • 63
  • 97
2

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Good point about design. A huge switch statement will almost always be better off in a loop, or something is wrong. – Pekka Jul 05 '10 at 11:47
2

The Switch Case Statement is an alternative to the if/else statement, which does almost the same thing. The Switch Case Statement executes line by line or statement by statement in other words, and once PHP finds a case statement that evaluates to true, it executes the code corresponding to that case statement. The fundamental difference between if/else and switch statements is that the if/else statement selects the execution of the statements based upon the evaluation of the expression in if statements, but the Switch Case Statement selects the execution of the statement often based on a keyboard command.

0

Don't forget, though, that a switch does not necessarily work as a simple if statement. Remembering that a switch does not require a break at the end of each case and leaving that break off allows you to 'fall through' to the next case, too, can allow some interesting and somewhat complex 'ifs'.

Narcissus
  • 3,144
  • 3
  • 27
  • 40