What is the difference between Switch-Case and If-Else in PHP?
I'm deciding whether to use if
/else
vs switch
/case
in a PHP site that I am writing and I was wondering if the开发者_StackOverflow社区re 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.
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).
Great .Net article about switch optimization: If vs. Switch Speed
Regarding php being interpreted, the PHP docs says: 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. 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.
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/
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.
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.
A switch-case statement switches on the value of a given expression only. 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.
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.
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. A switch statement can evaluate either an integer or a character. In the case of 'if-else' statement, either the 'if' block or the 'else' block will be executed based on the condition.
精彩评论