Thursday, November 27, 2008

PHP developer myths

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.

Read More...

No comments: