简体   繁体   English

PHP 中的 Switch-Case 和 If-Else 有什么区别?

[英]What is the difference between Switch-Case and If-Else in PHP?

我正在决定是否在我正在编写的 PHP 站点中使用if / elseswitch / case ,我想知道使用一个或另一个是否有任何好处,或者是否在某些情况下打算使用一个而不是另一个。

Interesting question because in a compiled languaged (or JIT'ed language even) there is a nice performance gain when using switch statements because the compiler can build jump tables and will run in constant time.有趣的问题,因为在编译语言(甚至是 JIT 语言)中,使用 switch 语句时有很好的性能提升,因为编译器可以构建跳转表并在恒定时间内运行。 Even switching on a string can be optimized as the string can be hashed.由于可以对字符串进行哈希处理,因此即使打开字符串也可以进行优化。 However, from what I've read, it appears php makes no such optimization (I'm assuming because it's interpreted and runs line by line).但是,从我读过的内容来看,php 似乎没有进行这样的优化(我假设是因为它被解释并逐行运行)。

Great .Net article about switch optimization: If vs. Switch Speed关于开关优化的优秀 .Net 文章: If vs. Switch Speed

Regarding php being interpreted, the PHP docs says: http://php.net/manual/en/control-structures.switch.php关于被解释的 php,PHP 文档说: http : //php.net/manual/en/control-structures.switch.php

It is important to understand how the switch statement is executed in order to avoid mistakes.了解如何执行 switch 语句以避免错误很重要。 The switch statement executes line by line (actually, statement by statement). switch 语句逐行执行(实际上是逐个语句)。 In the beginning, no code is executed.一开始,没有执行任何代码。 Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements.只有当 case 语句的值与 switch 表达式的值匹配时,PHP 才会开始执行这些语句。 PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. PHP 会继续执行语句,直到 switch 块结束,或者第一次看到 break 语句。 If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case.如果没有在 case 语句列表的末尾写 break 语句,PHP 将继续执行以下 case 的语句。

I also found several references all suggesting that if / else statements in php may actually be faster than switch statements (weird).我还发现一些参考文献都表明 php 中的 if / else 语句实际上可能比 switch 语句更快(奇怪)。 This probably is not true if you compile php (something I've never done, but apparently it's possible).如果您编译 php,这可能不是真的(我从未做过,但显然这是可能的)。

http://www.fluffycat.com/PHP-Design-Patterns/PHP-Performance-Tuning-if-VS-switch/ http://www.fluffycat.com/PHP-Design-Patterns/PHP-Performance-Tuning-if-VS-switch/

This article in particular, http://php100.wordpress.com/2009/06/26/php-performance-google/ , is interesting as the author compares internal php code of if vs switch and they are nearly identical.特别是这篇文章, http://php100.wordpress.com/2009/06/26/php-performance-google/ ,很有趣,因为作者比较了 if 和 switch 的内部 php 代码,它们几乎相同。

Anyways, I would say that any performance gain will be insignificant one way or the other, so it's more of a user preference.无论如何,我会说任何性能提升在某种程度上都是微不足道的,因此更多的是用户偏好。 If statements are more flexible and you can more easily capture ranges of values (especially large ranges) as well as do more complex comparisons whereas switch statements line up to exactly one value. If 语句更灵活,您可以更轻松地捕获值的范围(尤其是大范围)以及进行更复杂的比较,而 switch 语句只排列一个值。

A switch-case statement switches on the value of a given expression only. switch-case 语句只打开给定表达式的值。 This gets very convenient if you have a lot (I'd say even any more than two) possible actions to decide between.如果您有很多(我会说甚至超过两个)可能的操作来决定,这会变得非常方便。 So, if you have a number of discrete values of a variable and some code to execute for each one of those values, a switch-case statement is probably better.因此,如果您有一个变量的多个离散值和一些要为每个值执行的代码,那么 switch-case 语句可能会更好。

On the other hand, if you have a more complex test than simple equality, or if there are only two possibilities involved, an if-else is probably better.另一方面,如果您有一个比简单相等更复杂的测试,或者如果只涉及两种可能性,则 if-else 可能更好。

Note that in terms of actual behavior, they're identical;请注意,就实际行为而言,它们是相同的; it's just a question of which is more convenient for you, the programmer, and for whoever else will have to read or modify the code.这只是一个对您、程序员以及其他需要阅读或修改代码的人来说更方便的问题。

An if-else statement can evaluate almost all the types of data such as integer, floating-point, character, pointer, or Boolean. if-else 语句可以计算几乎所有类型的数据,例如整数、浮点数、字符、指针或布尔值。 A switch statement can evaluate either an integer or a character. switch 语句可以计算整数或字符。 In the case of 'if-else' statement, either the 'if' block or the 'else' block will be executed based on the condition.在“if-else”语句的情况下,将根据条件执行“if”块或“else”块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM