Hudzilla.org - the homepage of Paul Hudson
Contents > Performance > Optimising your code Wish List | Report Bug | About Me ]

18.1.23     Ignore the myths

This is NOT the latest copy of this book; click here for the latest version.

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 Performance Suite or PHP Accelerator, 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.





<< 18.1.22 Compile right   18.1.24 Avoid mod_access if you can >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 06 Sep 2008

I disagree with your comment about the difference between single and double quotes... Having tested this myself several times with several PHP releases, in situations where many quoted strings are used, single quotes could make a 20% difference!

Buddha443556 - 06 Sep 2008

Your correct stripping whitespace and comments does have little to no effect on PHP performance. Opcode Caching makes it totally worthless to do so. However, I've yet to see a Opcode Cache on a shared server and hoping to get lucky with the hard drive cache on a shared server is as bad as playing the Lottery. I heavily comment my code and get a least 50% size reduction in my scripts and that is 50% less data for the hard drive on a shared server to have to read. It's not about PHP performance, it's about hard drive performance.

I see enough of a performance increase on shared servers to continue this practice of stripping whitespace and comments. However, I would probably continue the practice even without a proformance increase just to save web space ... that ... and I hate wasting resources I could sell. ;-)



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow