The primary difference between PHP 3 and PHP 4 was that the previous release executed each line of code as it read it in the script, whereas the newer release converted the entire thing to a special internal representation of the code before executing it. The difference is crucial! In PHP 3, each line needed to be read as PHP script, converted into something that can be executed, then executed. If that same line was reached again (eg it was part of a function or inside a loop), it would be read from the script file again, converted into something that could be executed, and executed. As you can see, there was lots of repeated donkey work involved!
In PHP 4, the script is only read once, and converted to an internal representation of it. This results in much faster execution of scripts, particular where repeated code is found. This same paradigm is found in both PHP 4 and 5, and it has the interesting side effect that you can - with the right software - save the compiled version of the PHP script for later use. That is, PHP reads your script, converts it to the internal representation, then saves that representation. Then, when the script is called again, the pre-saved representation is used rather than the original script. This turns out to save a vast amount of processing power, and the best part is that because the PHP is saved before it is executed, pages cached in this way don't lose any of their "dynamicity" - they can still connect to databases and show user-specific content.
Although there are a selection of accelerators for PHP, few would argue that the real leader in the field is Zend's own product, Zend Server. The problem is that it's expensive, so unsurprisingly a lot of people use a built-in acceleration system called the Zend OPcache because it gives you a powerful and performant opcode cache for zero cost.
The best part about PHP code caches such as OPcache is that they automatically check for changes in the original PHP script, so that if you make an edit to the source code, it recaches it automatically. This means that from both a user and developer point of view, it's essentially transparent: you can't notice it's there, apart from the speed up.
The interesting thing about opcode caches is that the performance improvement it brings is usually at least 2x, rising up to about 8x for particularly complex scripts, and maybe more depending on your circumstances. For production servers, it's a no-brainer.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Caching PHP >>
Previous chapter: Server
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.