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.
About the Author
Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com
. He is the author of several popular books "Easy PHP Websites with the Zend Framework"
, "Easy PayPal with PHP"
, and "Beginning PHP and MySQL, Fourth Edition"
. Follow him on Twitter at @wjgilmore