Nurul’s Personal Blog

Hi there are some myths retarding optimization PHP scripts
Main article
1# Using single quotes for your strings rather than double quotes can yield a substantial speed boost.
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.
2# you should always take the comments and whitespace out of your scripts “for maximum performance”
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.
3# Use GLOBAL inside function to access external values
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.
4# Use unset() on complex variables
You may find people telling you to use unset() on complex variables so that PHP can free up the resources. This is quite an unusual one to tackle, because it is the sort of thing that should be true, and it is re-enforced by the fact that using a function to free up external resources, such as mysql_close() and imagedestroy(), does actually have an effect. I have never found unset() to actually free up memory when it is called. In fact, it tends to just burn up a lot of CPU time, which might have the opposite impact to what you want. Generally I think it is best to leave PHP to do the clearing up of standard variables, even if they are very large arrays, and stick to concentrating on more important things.
5# use $var rather “$var”
Some may try to convince you that using typing 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.


Comments on: "PHP developer myths (increase performance)" (3)

  1. Nice points Nurul, although I don’t quite agree on #1 with you. Nevertheless I do support your conclusion, performance hacks like these should not dominate the code you are writing. It can be taken care of much lower level(hardware etc.).

  2. I think these don’t add much to performance… I have one question…. if possible solve please… What is the reason to turning off register globals in php.ini file how it affects performance? and u didn’t mention about gz compression why?

  3. If that improve the performance of an application, still doesn’t worth waste of developing time.
    You may improve CPU and RAM, if you decrease developing time only one day.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: