PHP中的Switch-Case和If-Else有什么区别? [英] What is the difference between Switch-Case and If-Else in PHP?

查看:77
本文介绍了PHP中的Switch-Case和If-Else有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm deciding whether to use if/else vs switch/case in a PHP site that I am writing and I was wondering if there were any benefits to using one or the other or if there were certain instances where one was intended to be used rather than the other.

推荐答案

有趣的问题,因为在使用switch语句的编译语言(甚至JIT语言)中,由于编译器可以构建跳转表并将在恒定的时间运行.由于可以对字符串进行哈希处理,因此甚至可以优化字符串的切换.但是,据我了解,看来php并没有进行这种优化(我认为是因为它经过了解释并逐行运行).

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. 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).

有关开关优化的.NET很棒的文章: If vs. Switch Speed

Great .Net article about switch optimization: If vs. Switch Speed

关于被解释的php,PHP文档说: http://php. net/manual/en/control-structures.switch.php

Regarding php being interpreted, the PHP docs says: http://php.net/manual/en/control-structures.switch.php

重要的是要了解如何在以下位置执行switch语句 为了避免错误. switch语句逐行执行 (实际上是逐条陈述).一开始,没有代码是 被执行.仅当找到具有以下内容的case语句时, 匹配switch表达式的值,PHP是否开始执行 陈述. PHP继续执行语句,直到结束 或第一次看到break语句时.如果 您不会在案件陈述的末尾写一个break陈述 列表中,PHP将继续执行以下情况的语句.

It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). 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. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. 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.

我还发现了一些参考文献,它们都表明php中的if/else语句实际上可能比switch语句(怪异)要快.如果您编译php,这可能是不正确的(我从未做过,但是显然有可能).

I also found several references all suggesting that if / else statements in php may actually be faster than switch statements (weird). This probably is not true if you compile php (something I've never done, but apparently it's possible).

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

本文特别是 http://php100.wordpress. com/2009/06/26/php-performance-google/,很有趣,因为作者比较了if vs switch的内部php代码,它们几乎相同.

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.

无论如何,我想说,任何一种性能提升都将是微不足道的,因此更多的是用户喜好.如果语句更灵活,并且您可以更轻松地捕获值的范围(尤其是较大的范围)以及进行更复杂的比较,而switch语句恰好排在一个值上.

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.

这篇关于PHP中的Switch-Case和If-Else有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆