Switch vs if statements

后端 未结 6 1062
無奈伤痛
無奈伤痛 2020-12-17 10:15

I\'m in a dilemma. Which is best to use and why.. switch or if?

switch ($x) 
{
case 1:
  //mysql query 
  //echo something
  break;
case 2:
  //mysql query 
         


        
6条回答
  •  北海茫月
    2020-12-17 10:45

    both of the statement are decision making statement by comparing some sort of the parameters and then show the results. the switch statement is no doubt faster than the if statement but the if statement has a bigger advantage over the switch statement that is when you have to use logical operations like (<,>,<=,>=,!=,==). whenever you came across with the logical operations you will be struck off by using switch statement but don't worry there is another way which can be use to reach at your goal that is if-statement or if-else or if-else-if statement. I'll suggest you to use if-else most of the time rather than the switch one.

提交回复
热议问题