Competition is helping to drive big performance gains in PHP. Alternative ways of running PHP are becoming viable and with them is coming accelerated speed.
Why worry about performance?
PHP is not the fastest language in which we could write web applications, yet we continue to do so for many other reasons. Pure speed of a language is rarely the main deciding factor for many projects. Developer productivity, for one thing, is usually more important. And in many applications the bottlenecks will not be in the application code; instead it is where interaction with other systems takes place. For example, communicating with databases, APIs, and message queues all take time.
So why worry about the speed of the language at all? Well, application architecture is improving and we are finding ways to avoid all those other bottlenecks. For example, better caching, asynchronous processing and eventual consistency all help, but also make application code performance more important. Trying to gain speed through profiling and optimising code can be a long and tedious process. Thankfully, improvements in the speed of the language itself give us an improvement in these other areas for free.
So why is PHP slow in comparison with some other languages? PHP is a dynamic, interpreted language. This means that it is not compiled to machine language but rather read at runtime. PHP also has a share-nothing architecture, so on every request it interprets everything from fresh. The consequence of this is that performance is not as good as for compiled languages, but it also allows for features that compilable languages do not.
Not needing to compile PHP can help with developer productivity in a few ways. It allows for shorter feedback cycles when developing – the results of changes to the code can be seen immediately without any compilation stage needing to be run first. There is less need to worry about garbage collection and memory use. Debugging of runtime errors is made easier, because you can directly identify where they occur in the source code. It also allows for dynamic code such as variable variables, dynamic types, and so on, though care needs to be taken with these to avoid making your application difficult to test.
A bit of history
PHP grew out of some CGI binaries written in C by Rasmus Ledorf. To be able to embed HTML and other web-specific tasks like form handling, he added parsing of a simple Perl-like syntax. This parser for PHP was rewritten by Zeev Suraski and Andi Gutmans for the release of PHP3. Version 4 of PHP saw the Zend engine - a completely new way of running PHP - introduced. PHP 5 brought with it the 2nd version of the Zend engine. Performance has increased through all these releases.
The Zend engine parses PHP code and turns it into opcodes, which are then interpreted. A big performance boost has long been available by caching this conversion to opcodes. Until version 5.5 of PHP, this caching needed extensions to PHP - the most used being APC. As of 5.5 PHP opcode caching is now part of the core.
Zend PHP became the de facto version of the language and the engine on which it runs. There have been alternatives to this over the years but nothing that has caught on with any real success.