Speeding up your PHP code

PHP isn’t the fastest language, but it’s safe to say that it’s one of the most popular languages. It’d be great if the language is at least faster than what it is now.

Caching and code optimization

There have been many attempts to speed up PHP performance. These improvement focuses haven’t been always the same. For example, eAccelerator caches up PHP scripts just as if they were compiled as well as optimizing scripts themselves. The caching helps reduce the server load. eAccelerator is under active development. The current version ( supports PHP 5.3 as well as 5.1 and 5.2.

Alternative PHP Cache (APC) is a widely used library also focusing on caching PHP code. APC is a PECL extension, so the installation is as easy as pecl install apc. In addition, the project is well documented on php.net.

A similar, but commercial solution for caching and code optimization is Zend Server. The price range is $1,200 to $3,200 depending on the support level. Zend Server offers more than caching and optimizing code. It helps you analyze your PHP scripts by showing execution time, errors and memory usage in a nice graphical user interface. It also automagically executes page/partial level caching. For example, if your user hits a URL and you want to serve exactly the same HTML from last time, Zend Server avoids checking the application code and database queries to reduce the server load. In most of the systems you need to implement page caching and fragment caching on your own, but Zend does it for you. Zend Server has more features than those mentioned above. As an added bonus, Zend Server works elegantly with Zend Framework.Read the full coverage at Zend.

Zend Server Community Edition is free. You can find the difference between the community edition and commercial edition here. You might very well want to try the community edition first before purchasing the commercial edition.

A relatively new project on this area is XCache. XCache is written by a Lighttpd developer who came out of frustration with other caching solutions. The development is quite active active. For example, it supports newer versions of PHP as soon as they come up. The developer emphasizes its stability, so we can expect that the support for newer PHP versions comes fast and well.

Compiling into other languages

Well, caching is great. But if there are other faster languages like Java, why don’t we just convert PHP into such languages? Some projects aim to speed up PHP by compiling PHP into another language. Languages and frameworks used in enterprise markets are usually fast and capable. Quercus compiles PHP into Java while Phalanger compiles the code into .NET. A notable newcomer is HipHop for PHP by Facebook, which is the most popular social networking site in the world with more than 500 million users.

HipHop changes PHP code into C++ for better performance. The project is opensource and hosted on GitHub. The talk by a Facebook developer discusses the details of the project. There are many issues left open on GitHub, and the official mailing list doesn’t seem quite active. In addition, PHP experts are dubious about its effectiveness. Rasmus Lerdorf, the PHP creator, in his interview with ReadWriteWeb, states that HipHop is not a solution that solves everything. He also suggests that developers first employ basic techniques to make the web apps faster. Things like database query cache, database query optimization and frontend optimization(CSS, JavaScript, HTTP requests) as well as PHP code caching may be the most useful and important to work on until your app hits the limits of the current technologies.

Just writing code

If you don’t have a permission to change your server setting, you could improve the speed.

Stephen Ward shares how to write efficient code with PHP. He gives advice such as “Use echo instead of print().” and “Ditch double quotes for single quotes.”. But as pointed out by the commenters, some of these tips are probably outdated or by being taken care of by cache optimizers mentioned early in this article.

This post is also available in other languages.