Switch vs if statements

后端 未结 6 983
無奈伤痛
無奈伤痛 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:33

    Switch is better when there are more than two choices. It's mostly for code readability and maintainability rather than performance.

    As others have pointed out, your examples aren't equivalent, however.

提交回复
热议问题