Did you know that using single quotes for your strings rather than double quotes can yield a substantial speed boost? If you did, I am sorry to disappoint - this is one of the most commonly repeated performance myths about PHP. Whether you use double quotes or single quotes is pretty much beside the point - yes, there is a performance boost to use single quotes, but it is much less than 0.01%, and it is generally just not worth the extra hassle. Many people use double quotes for everything, and that is fine - use whatever you feel most comfortable using, because it will not affect the speed of your script. Note that this rule does become true when variables are being used - for maximum performance (and minimum readability, mind you!), you should use concatenate variables outside of strings rather than use variable substitution.
Similarly, if you hear people tell you how you should always take the comments and whitespace out of your scripts "for maximum performance", tell them where to go - comments and whitespace have such a minute effect on the performance of your PHP scripts that it is not worth considering. Furthermore, if you use a PHP code cache system like Zend OPcache, comments are stripped out for you in the cached version, meaning they have no impact at all .
There is a lot of fluff concerning the supposed performance hit of using GLOBAL inside functions to access external values. Some people will tell you it is faster to use GLOBAL, others that it is faster to pass a parameter in, and still others that it is faster to use a static or class variable to store the value in. I find them all to run at the same speed unless there are exceptional circumstances, and I would recommend you use the way that suits you best on the basis of ease of use as opposed to performance.
Some may try to convince you that using print $var is better than print "$var", and indeed it is - but not because one is faster. Instead, the first option looks a little neater, and I would recommend it for that reason only. Again, this is a style point - don't let people tell you that either is faster than the other. This is particularly irrelevant if you use a PHP code cache, which will treat both code lines in exactly the same way.
Finally, be wary that a lot of the performance advice out there no longer applies as of PHP 5. For example, some people used to recommend putting methods as low down in your class hierarchy as possible on the grounds that function calls in derived classes are somehow faster than they would be if they were in the grandparent class. If this was true in a previous version of PHP, it is certainly no longer true now and you should stick with the OOP guidelines.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Avoid mod_access if you can >>
Previous chapter: Compile right
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.