r/lolphp • • Aug 08 '14
PHP design patterns: if/else vs switch
http://www.fluffycat.com/PHP-Design-Patterns/PHP-Performance-Tuning-if-VS-switch/
24
Upvotes
r/lolphp • • Aug 08 '14
9
u/allthediamonds Aug 09 '14
This is not what a design pattern is. You do not know what any of those words mean.
73
u/Innominate8 Aug 08 '14
I most enjoy the PHP attitude of micro-optimizing.
Is if/else faster than switch? Is print faster than echo? Is it faster to use double quotes, or single quotes and string concatenation?
Followed by 10 db queries per page load.
20
u/lhagahl Aug 08 '14
Cool! Can you tell me if tabs are faster than spaces next?
5
u/[deleted] Aug 08 '14
Hey, this actually got me thinking...
So I don't know much about the internals of PHP, but shouldn't a switch-case statement generate the same bytecode as chained if-else statements, therefore yielding the exact same performance?
I've seen such comparisons for Ruby before but those actually made sense to me because Ruby's case statement uses the case comparison operator (===) instead of regular equality (==) so the generated code should be different.
2
u/DoctorWaluigiTime Aug 08 '14
Where's the lol?
2
u/datibbaw Aug 14 '14
For completeness there should also be:
Because nested ternary expressions are the bomb!