#native_company# #native_desc#
#native_cta#

6 PHP Performance Tips for Producing Highly Optimized Code

By W. Jason Gilmore
on July 12, 2011

PHP is responsible for powering an extraordinarily large segment of the Web, driving significant parts of many of the world’s most trafficked websites, among them Facebook and Yahoo. Facebook’s reliance on PHP is so great that they’ve even gone so far as to create HipHop for PHP, a utility that converts PHP code into highly optimized C++, resulting in the ability of the Facebook API tier to double its performance while reducing CPU usage.
While HipHop may seem a rather esoteric solution to the performance problem, a method lies within the perceived madness; maintaining the website logic using PHP code allows Facebook to maintain a code base which remains accessible to a larger number of developers, with the added bonus of being able to deploy blazing fast C++ code.
Of course, incorporating HipHop into your infrastructure might not be ideal, but that shouldn’t stop you from striving to produce highly optimized PHP code. Read on to learn more about several straightforward steps you can take to boost PHP performance.

PHP Performance Tip #1. Upgrade Your PHP Distribution

Simply upgrading your PHP distribution is without a doubt one of the easiest ways to improve PHP performance. In addition to the usual bug fixes and new features, each new PHP release ships with optimization improvements which can produce noticeable performance enhancements. Although the post is a few years old, prominent PHP community member Sebastian Bergmann illustrates the stark advantages to be had even when migrating from PHP 5.2 to 5.3.

PHP Performance Tip #2. Use a Profiler

Your website is running slow. But what is causing it to run slow? Today’s complex Web application environments produce more suspects than a murder mystery. One possibility is of course the PHP code itself, or any of the components the PHP code plugs into. Use a profiler to determine which parts of your code are causing bottlenecks which contribute to the website’s overall performance degradation.
The Xdebug PHP extension provides a great tool for both debugging and profiling code, offering PHP developers the ability to peer directly into the script’s execution stack and review comprehensive statistics regarding execution time. This data can then be imported into a visual tool such as KCachegrind for convenient analysis. For instance, Figure 1 presents KCachegrind-formatted profile data for one of GameNomad’s internal pages.



Click here for larger image


Figure 1. Reviewing the GameNomad Execution Stack
To learn more about Xdebug and KCacheGrind, check out my PHPBuilder article about profiling PHP code.

PHP Performance Tip #3. Tone Down Error Reporting

PHP offers a powerful array of error detection and logging features, which allow you to monitor and log errors to wildly varying degrees of sensitivity, ranging from critical errors to relatively minor runtime notices. In fact, thirteen separate reporting levels are supported, and you can even mix and match these levels to produce customized detection and reporting behaviors.
While this feature is supremely useful during the development phase, the sensitivity level should be significantly reduced or even hopefully disabled in production, as each detection incident incurs a cost. Always strive to develop and test your applications in an environment which matches that used in production so as to ensure the maximum number of errors are identified and resolved before deployment.