Join Justin Yost for an in-depth discussion in this video Upgrade PHP, part of PHP: Performance Optimization.
- [Instructor] One of the best performance improvements you can do for PHP is to upgrade the version of PHP you are on. Especially if you're on a PHP 5 version, upgrading gives you a lot of performance benefits. As I'm speaking in the second half of 2017, PHP 7.1 is the current stable version of PHP. PHP 5.6 is no longer in active support, meaning it no longer receives performance improvements and is expected to only get bug fixes til the end of 2018.
PHP 7.0 is also on its way out the door, with active support going away by the end of 2017. 7.2 is currently in active development and alpha releases have already started. Like everything else you are doing, whether it's targeting a version of a framework or upgrading your servers operating system, targeting the active version is going to constantly change. PHP should be something put on a regular upgrade cycle. Every new release, not only fixes minor bugs and adds new features, but yes, indeed features speed improvements.
PHP 5.6 wasn't ever a sluggish version of PHP especially compared to much earlier versions, but the PHP core team did some serious work to improve the performance of PHP in 7.0. Mandelbrot is a set of complex numbers that generates a fractal pattern. Bench and micro-bench are standard PHP performance measurement scripts included in the PHP source. The count here in this case is the number of seconds it took for these particular programs to run.
You can see PHP 7 is a massive boost in performance over PHP 5.6. Maybe you don't care about some random benchmarks and math equations. What about some real world software? So here are few different application performance measurements on PHP thanks to Zend. You can see every real world application sees an increase in the number of requests per second they were able to process. So what changed in PHP 7.0 to give us such a bump in performance? The short version is a project entitled PHPNG or PHP Next Generation.
This gave PHP a huge performance improvement and building off of it should keep PHP moving faster for years to come. PHPNG encompasses a lot of little improvements to the underlying PHP source. While not adding any new features to PHP itself, instead improving the speed of PHP itself. Most of this consists of improvements to the memory manager in PHP, the parameter parsing, the hashtable API, as well as using more references over copying.
There's also a fair bit of other improvements that were made. At the end of the day, all you really should care about is PHPNG gives the PHP core team a huge performance win without breaking PHP. Some of you might be asking the question of what happened to PHP 6, how did we go from PHP 5 to PHP 7? There's a fun story here actually. PHP attempted, in 2010, to build a new version of PHP that would include full Unicode support in the language itself.
In other words, you could use variables and method names that included full Unicode characters such as emojis, or perhaps more importantly, use you own local language such as Hebrew, or Japanese, or whatever in your PHP code. It would have enabled more than just that, but that would have been the most visible day to day change in PHP. But what happened? Well Unicode is really, really, really, really hard, so at the point where it stalled out was also the point at a lot of books, and a blog posts, and a lot of videos had already been written and recorded talking about PHP 6.
So when it came time to name the next version of PHP, the PHP core team decided to jump version numbers from 5 to 7 to provide a clean break and ensure no one would be confused about which version of PHP you were talking about. So now that you're sold on upgrading to a new version PHP, what's the way to do it? Well if you're running unit tests on some sort of continuous integration service like Travis or Circle CI, or some similar testing, you can test your application on a different version of PHP pretty easily.
Simply add that new version of PHP to your listing of PHP versions to test against. If you aren't doing this, the next best solution is probably to spin up a new environment of your development infrastructure using a newer version of PHP. Keep in mind, you should try this out before rolling a new PHP version into production. For one, you want to verify you don't have any breaking issues in your code base. And secondarily, remember to test and measure, and see what performance improvements you actually come across. Some of you might be asking a question about HHVM, so let's talk about it.
Way back in 2010, there was a small little company called Facebook that was taking over the web and growing super fast, and growing so fast they were growing faster than they could reliably scale their servers. Facebook was at the time powered by PHP. Facebook realizing this, that if they could turn their PHP code in C++ code, they could run that C++ code faster on the same number of servers. Facebook, because of this, wrote HipHop for PHP or HPHPC.
It is, in effect, a PHP transpiler to C++. Now what this means is, is that if you have a certain PHP program you could compile it into a C++ code base and it would do the exact same work. In other words, it compiles and translates PHP source directly into C++ source code, and now that you have C++ code you compile that into a binary and run it as an executable binary directly on your servers.
Recall our process with the Opcode cache, notice if we have a binary executable, we can skip over this Opcode conversion point, but we also gain performance benefits from using a faster language like C++. In addition, we can tune that C++ code to be even faster if we need to. So Facebook was doing this and it was working okay until around 2013 at which point Facebook dropped HipHop for PHP in favor or HHVM or the Hip Hop Virtual Machine.
Many of the reasons for this wound up being that Hip Hop itself did not fully support all of PHP's features, so instead the Hip Hop Virtual Machine was released. A virtual machine is an emulation of a computer system. Java runs, for instance, in a virtual machine. The goal here is an environment in which you can specifically tune to best support the underlying hardware better than you can in your top level language. HHVM is a virtual machine for PHP as well as supporting a secondary language, HACK.
HACK is a language that Facebook built to operate seamlessly with PHP but provide improved performance and features. So the real question is, should you use HHVM? In general, probably not. Facebook uses it and supports it really well. It's still in active development and seeing regular updates, however it's not as widely supported as PHP itself. For instance, though there are official packages released for Debbie and in Ubuntu, only the most recent Ubuntu and Ubuntu LTS releases received official blessing.
You might want to try it out and see if you can get your application running on HHVM, just to see if it'll both work with and fit with your infrastructure, but in general unless you have some specific use case, you should consider sticking with PHP itself. Here's the big takeaway, if you're on PHP 5, you should really upgrade to PHP 7 before doing almost anything else in this course. And secondarily, if you are on PHP 7 already, you should make a plan to regularly update PHP versions as they're released.
- General optimization techniques and tools
- PHP and Xdebug
- Opcode cache
- Optimization in PHP
- Upgrading PHP
- Macro vs. Micro optimization
- HTTP caching
- HTTP compression