What is the difference between Switch-Case and If-Else in PHP? -
I am deciding that Interesting questions when using a compiled languaged (or even in JIT'ed language) switch statement There is a good performance advantage as the compiler can build leap tables and run in constant time. The switch on the string can also be optimized because the string can be washed. However, what I've read appears to be that PHP does not make any such optimization (I believe it because it means and runs the line by line). Great article about switch optimization: About PHP explanation, PHP Docs says: Switch to avoid mistakes It is important to understand how the statement is executed: The switch states the line through the line (in fact, 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, then the PHP statement starts executing. The PHP switch continues to execute the statement by the end of the block, or first sees a break statement if you do not write a break statement at the end of the statement list of a case, then PHP executes the following case statements Will go away. I have also received many references that are suggesting that the / else statement in PHP may actually be faster than the switch statement (weird). This is probably not true if you compiled PHP (something I have never done, but apparently it is possible) This article is particularly interesting because the author compares the internal PPC code versus the switch and they are almost identical. Anyway, I would say that the benefit of any display will be equal to one way or the other, so it is more of a user priority if the statement is more flexible and you can more easily value (especially Large range) and also make a more complex comparison, while the switch takes the statements to a single value. if /
else vs < In a PHP site, code> switch /
case I am writing and I was thinking that if there is any benefit to use one or the other, or if there were some such examples Where one was intended to be used compared to the other.
Comments
Post a Comment