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.
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.
PHP Performance Tip #4. Take Advantage of PHP's Native Extensions
PHP has long been accused of including everything but the kitchen sink, and in recent years the development team has made great strides in terms of stripping the core distribution of optional and redundant features (such as deprecating the redundant POSIX Regex library in preference of the PCRE library). Even so, the sheer number of available libraries and other extensions can be overwhelming, leading some developers to consider implementing their own solutions rather than invest the time investigating what's already available.
For instance it might be tempting to spend a few hours writing a custom library capable of parsing XML, despite the availability of SimpleXML. Doing so ignores the many hours of troubleshooting and optimization already put into the native extensions, thereby almost guaranteeing the substandard outcome of any such custom solution. For this reason and others you should always investigate the availability of an existing library or extension before setting out to reinvent the wheel.
PHP Performance Tip #5. Use a PHP Accelerator
Barring outside interference, a PHP script executes after being "compiled" by the PHP engine, which turns the PHP code you've written into machine language, known as operation code (opcode). If the PHP script repeatedly churns out an identical result, then why not skip the compilation process altogether?
You can do exactly this using a PHP accelerator, which caches a PHP script's compiled machine code, allowing the code to immediately execute upon request rather than after a potentially expensive compilation process.
Several opcode caching solutions are available to PHP developers, including notably APC (Alternative PHP Cache), an open source accelerator available for installation via PEAR. Another popular solution is Zend Server, which incorporates not only opcode caching, but also full and partial page caching utilities.
PHP Performance Tip #6. Avoid Expensive Operations Through Memory Caching
PHP often plays a central role in the retrieval and analysis of data involving potentially expensive operations which contribute to performance degradation. Such operations are often unnecessary, particularly when they involve the repetitive retrieval of generally static data from a database. Consider caching such data for even short periods of time using the popular Memcached extension.
Working in tandem with the libMemcached library, the Memcached extension stores cached data in RAM, dramatically improving retrieval times over expensive database operations. It's also possible to define cache expiration times which generally correspond to the data's volatility, thereby helping to ensure users are always provided with the most up-to-date information possible.