Tips for PHP website speed optimization

PHP is great for writing quick dynamic stuff for your website.

Just a couple of lines of code can be written in few mins to insert or retrieve data from database.

The negative side is that each request for a dynamic page can trigger multiple database queries, processing of output and finally formatting to display on browser.

This process can eventually be slow on larger sites or slower servers.

It’s no wonder that performance and speed are very popular quality goals when it comes to PHP.

And when it comes to code quality, PHP obeys the same rules as any other programming language, which can lead to a relatively small issue in the source code causing a lot of difficulty for the user.

Below are few techniques which can be used to improve your website performance:

1.  Load time:  The first step is to determine what that load time is. What you experience loading the site may be different than somebody on a different Internet connection, using a different browser or in a different physical location. There are quite a few tools out there for page load speed testing.

2. Use caching : Making use of a caching module, such as Memcache, or a templating system which supports caching, such as Smarty, can help to improve the performance of your website by caching database results and rendered pages.

3. Use output buffering : PHP uses a memory buffer to store all of the data that your script tries to print. This buffer can make your pages seem slow, because your users have to wait for the buffer to fill up before it sends them any data. Fortunately, you can make some changes that will force PHP to flush the output buffers sooner, and more often, making your site feel faster to your users.

4. Avoid string concatenations in loops : When placed in a loop, string concatenation results in the creation of large numbers of temporary objects and unnecessary use of the garbage collector. Both consume memory and can dramatically slow down script execution.

5. Don’t pass function variables by reference : In most cases, functions only need to use the values passed by their parameters, without altering the values of such parameters. In such cases, you can safely pass those parameters by reference. By this we can avoid memory-intensive copies and increase application performance.

6. Don’t use relative paths : For better performance, it is highly advised to try and minimize the use of relative paths for file inclusion. The general mechanism for relative path inclusion will search for default include paths, then continue with current directory, and so on. In such cases, a file search will take more time. However, if you need to use relative paths, the best practice is to declare the constant “ROOT” which defines the root and use it afterwards.

7. Use echo instead of print(): As a language construct rather than a function, echo has a slight performance advantage over print().

8. Avoid using regular expressions : Regular expressions are very useful, but also very time-consuming. For this reason, limiting their use is, to say the least, highly recommended. Regular expressions are known to be much slower than their PHP counterparts.

9. Don’t use SQL queries within a loop : A common mistake is placing a SQL query inside of a loop. This results in multiple round trips to the database, and significantly slower scripts.

10. Avoid needlessly copying variables: If the variable is quite large, this could result in a lot of extra processing. Use the copy you already whenever possible.

Posted in: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *