PHP performance tips from Google

Tags: php

Note- This article is meant only for the beginners to intermediates

PHP is one of the much sought after language used by many of the popular web developers around the world.  Hence Google has come up with a few tips for the better performance of PHP scripts, in one of their recent articles. These tips will help the web developers develop a quick and painless method to improve PHP programs.

‘Profile your code to identify bottlenecks’

Premature optimization is considered to be one of the root causes of the lower performance levels in PHP. Experts say that this has to be clearly noted while trying to make faster websites. Try to look in to the reason for the slowness, before changing your codes, making a clear idea of whether the issue is related to database or network.

Upgrade the version of PHP used

PHP has undergone some significant changes, in performance, in the recent years. So if you are still struggling with the older versions, look out for the possibilities of an upgrading before optimizing your code.

‘Use caching’

Certain developers say that use of a caching module, like Memcache, or a ‘templating system’ like Smarty which supports caching will surely improve the performance of the websites, as the help in caching the database results as well as rendered pages.

‘Use output buffering’

The use of ‘memory buffer’ has certain limitations, says Google. A memory buffer is used to store the data that the script tries to print. ‘Memory buffers’ may slow down the performance of your pages, making the users wait until the buffer gets filled up to get any needed data.
The Output Control functions help users to control the output sent from the script. This is highly useful to the PHP users, especially when a header has to be sent to the browser, when your script starts outputting data.

Don't copy variables unnecessarily

In your attempt to get a ‘cleaner’ code don’t copy variables unnecessarily. Some PHP novices try to copy predefined variables unnecessarily resulting in ‘doubled memory consumption’ that slows the scripts soon.

Avoid doing SQL queries within a loop

A very common mistake done by PHP novices is the placing of SQL queries inside a loop, resulting in multiple round trips to the database considerably slowing the scripts.

‘Use single-quotes for long strings’

PHP engine permits the use of both single and double-quotes for string variable encapsulation, but both have their own differences. Change of double-quotes to single helps in preventing the PHP engine from parsing the string, during the attempt to expand variables.

‘Use switch/case instead of if/else’

Use of switch/case statement in place of if/else, while testing against a single variable, often gives better results.

Here is a short video on the topic - http://www.youtube.com/watch?v=Sibg0T3wKs4&eurl=http%3A%2F%2Fcode.google.com%2Fspeed%2Farticles%2Foptimizing-php.html&feature=player_embedded

Comments and Feedback

Post your Comment

Type your comment here*


 

Verification Code Image

Back to Main Top of Page