1

I only have one case in switch statement

switch ($coreName) {
    case 'stock':
        $result['website'] = ($result['website']) ? $websiteStatus[$result['website']] : "";
    break;
}

My question is this a good practice or shall I use an if statement for this? Any performance differences or anything?

3
  • In this case it's just a convoluted way of writing an if statement, so you kind of answered your own question. Also, as long as you have doubts about the basics of programming, don't get caught up in the performance story because it will almost never matter. Commented Jun 29, 2015 at 9:54
  • If you expect to add other cases later, it's perfectly OK. Commented Jun 29, 2015 at 9:57
  • 1
    @DmitryGrigoryev I'm sorry but that's not really good advice. I can "expect" a great many things about a project. If all these expectations are to be prepared in code, you risk designing and developing a spaceship to fry an egg. Your code should ALWAYS follow the proverbial JEEP principle (just enough essential parts) to make it work. When the requirements change, THEN is the time to swap out and/or expand. This is always important and especially for novices who need to learn best practices. Either you already have multiple cases that warrant a switch or you don't and you use an if. Commented Jun 29, 2015 at 10:05

4 Answers 4

2

Use if else Only when :

1. you have only 2 or more conditions OR You have multiple conditions in single if else.

And use switch when

1. You have to compare `ONE` variable value against multiple.

In your case if else is better

1
  • The first condition makes no sense to be honest. What if you have multiple boundaries?
    – skubski
    Commented Jun 29, 2015 at 9:57
2

Nothing speaks against this from a technical point of view.

So the question to answer is: why did you implement a switch statement instead of an if conditional?

I'd say the default should be an "normal" if condition, unless special reasons point to a switch instead. The two primary reasons might be:

  • extensibility (for later additions)
  • readability (for other coders)

Especially the first case should be considered here. If it might become necessary to extend the handling, then certainly using a switch right away is a good practice.

1

If you go to phpBench.com and scroll down to see the results between if and case, you can see that using an if statement is slightly faster, but the results aren't too different. (Screenshot below)

enter image description here

You may as well use an if as it is easier to read, less code and is still faster (even by only a tiny amount).

if($corename === "stock") {
  $result['website'] = ($result['website']) ? $websiteStatus[$result['website']] : "";
}

case should only be used when you compare multiple values rather than just one. This is the same as using elseif. Using if is the most suited for this specifically.

0

Why not simply do a simple if, like this :

if($coreName == 'stock')
    {
        $result['website'] = ($result['website']) ? $websiteStatus[$result['website']] : "";
    }

Hope this helps.

2
  • Yes but are there any advantages if we use if over switch in this case.
    – Turn
    Commented Jun 29, 2015 at 9:56
  • Switch is for when you have to test multiple cases. In your specific case, it's not appropriate. In terms of performance, if ... else is pretty god (@BeatAlex's answer details it more than I can). Commented Jun 29, 2015 at 10:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.