php Switch 与 if 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4241768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch vs if statements
提问by CyberJunkie
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
//echo something
break;
}
...
...
if ($x == 1) {
//mysql query
//echo something
}
if ($x == 2) {
//mysql query
//echo something
}
回答by alex
They have different meanings.
它们有不同的含义。
The first example will stop when the condition is met.
第一个示例将在满足条件时停止。
The second will test $x
twice.
第二个将测试$x
两次。
You may want to make your second if
an else if
. This will mean the block will be skipped as soon as one condition evaluates to true.
你可能想让你的第二if
个else if
. 这意味着只要一个条件评估为true就会跳过该块。
But if you are wondering which one is fastest, you should instead think which one most effectively communicates what I want to do. They will both most probably end up as conditional jumps in the target architecture.
但是,如果您想知道哪一个最快,您应该考虑哪一个最有效地传达我想要做的事情。它们很可能最终都成为目标架构中的条件跳转。
Premature optimisation... (you know the rest :P )
过早的优化......(你知道其余的:P)
回答by ddrace
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.
然而,正如其他人指出的那样,您的示例并不等效。
回答by iphonemaniac
The switch statement will be faster because it only checks the value once.
switch 语句会更快,因为它只检查一次值。
回答by John Hartsock
First what you have is not the same
首先你拥有的不一样
switch(value)
case condition1:
break;
case condition2:
break
if (condition1) {
}
if (condition2) {
}
These are synonymous
这些是同义词
switch(value)
case condition1:
break;
case condition2:
break
if (condition1) {
}
else if (condition2) {
}
Second if you are talking about my second example where the two are synonymous. Then using switch
statements can ease some pain in coding lots of if ..else if
statements....
其次,如果您在谈论我的第二个示例,其中两者是同义词。然后使用switch
语句可以减轻编写大量if ..else if
语句时的一些痛苦......
Switches in my point of view can also provide a bit more readability. But even then there are times when using if...else
is simply better especially with complex logic.
在我看来,开关也可以提供更多的可读性。但即便如此,有时使用if...else
会更好,尤其是对于复杂的逻辑。
回答by jtdubs
If you have a single variable, a set of possible values, and you want to perform different actions for each value, that's what switch-case is for.
如果您有一个变量,一组可能的值,并且您想对每个值执行不同的操作,这就是 switch-case 的用途。
Switch statements make it more obvious that you are merely determining which of the allowed values a variable had.
Switch 语句更明显地表明您只是在确定变量具有哪些允许值。
If you have more complex conditions, or multiple variables to consider, use if-statements.
如果您有更复杂的条件或要考虑的多个变量,请使用 if 语句。
回答by AliMohsin
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.
这两个语句都是通过比较某种参数然后显示结果的决策语句。switch 语句无疑比 if 语句快,但是 if 语句比 switch 语句有更大的优势,那就是当你必须使用 (<,>,<=,>=,!=,==) 等逻辑运算时. 每当您遇到逻辑操作时,您都会被使用 switch 语句所淘汰,但不要担心还有另一种方法可以用来达到您的目标,即 if-statement 或 if-else 或 if-else-if陈述。我建议您在大多数情况下使用 if-else 而不是 switch 。