I decided to prioritize items related to site performance in my huge list of pending tasks. When evaluating how much work each site would take, I ended up discovering that update my websites from PHP 5.6 to PHP 7.2 would actually be a low hanging fruit!
It turned out that I would only have to do some very simple things to have my websites running the PHP 7.2 from the outdated PHP 5.6.
Migrate WordPress from PHP 5.6 to PHP 7.2 is much easier now, many years after major PHP 7.0 was released. Most WordPress plugins and themes are already compatible. How to find out what do you need to update?
To check for incompatible code on theme and plugins I decided to rely on an automated verification using the plugin PHP Compatibility Checker. As far as I understood it statically analyzes the code looking for smells of outdated lines.
Then I fixed the errors by myself or replaced some plugins.
The most common errors I found were:
- Extension ‘mysql_’ is deprecated since PHP 5.5 and removed since PHP 7.0; Use mysqli instead
- Extension ‘ereg’ is deprecated since PHP 5.3 and removed since PHP 7.0; Use pcre instead
- Function ereg_replace() is deprecated since PHP 5.3 and removed since PHP 7.0; Use preg_replace() instead
- Using ‘continue’ outside of a loop or switch structure is invalid and will throw a fatal error since PHP 7.0
- Function split() is deprecated since PHP 5.3 and removed since PHP 7.0; Use preg_split() instead
And also some warnings that I also fixed:
- Use of deprecated PHP4 style class constructor is not supported since PHP 7.
- Function create_function() is deprecated since PHP 7.2; Use an anonymous function instead
Locally, I tested the code using WordPress on Docker with PHP 7.2 and also with PHP 5.6. The code after the fixes is compatible with both versions.
The next steps were:
- change the Apache domain configuration on Dreamhost to PHP 7.2
- replicate the same Apache configuration for the HTTPS version of the site.
- commit the code so that it could be uploaded to production using some CI\CD pipelines on Gitlab!
- restated Apache on Dreamhost.
Testing local with Docker
And about the performance after this upgrade?
I tested repeatedly and, on average, this is what I found!
PHP 5.6 benchmark:
Time to perform: Math test : 0.25 seconds
Time to perform: StringManipulation test : 0.48 seconds
Time to perform: test Loop test : 0.21 seconds
Time to perform: test IfElse : 0.26 seconds
Total time (all PHP tests) : 1.20 seconds
PHP 7.2 benchmark:
Time to perform: Math test : 0.06 seconds
Time to perform: StringManipulation test : 0.18 seconds
Time to perform: test Loop test : 0.08 seconds
Time to perform: test IfElse : 0.07 seconds
Total time (all PHP tests) : 0.39 seconds
Results: PHP 7.2 is 3x faster than 5.6!
Benchmarking in production
This is a very superficial benchmark provided by the WordPress plugin PHP/MySQL CPU performance statistics, but I still wanted to evaluate in a test site in the production environment.
PHP 5.6 benchmark:
Time to perform: Math test : 0.72 seconds
Time to perform: StringManipulation test : 1.07 seconds
Time to perform: test Loop test : 0.48 seconds
Time to perform: test IfElse : 0.98 seconds
Total time (all PHP tests) : 3.25 seconds
PHP 7.2 benchmark:
Time to perform: Math test : 0.14 seconds
Time to perform: StringManipulation test : 0.42 seconds
Time to perform: test Loop test : 0.19 seconds
Time to perform: test IfElse : 0.55 seconds
Total time (all PHP tests) : 1.30 seconds
Results: PHP 7.2 is 2.5x faster than 5.6!
WordPress is faster with PHP 7.2?
The result of these tests sounds promising… so now I decided to test the actual WordPress site and evaluate how long it would take to generate the same page on each PHP version.
I disabled the cache so that pages would be fully generated each time and in the first measure I did was the TTFB or Time to First Byte.
This measure also includes the latency of your connection to the web server. It is composed by a roundtrip from your computer to the server and back, plus the time server took to respond.
TTFB is made up of the socket connection time, the time taken to send the HTTP request, and the time taken to get the first byte of the page. As I am really close to the web server and latency is below 20ms, almost two orders of magnitude below than the expected server processing time, I will pretty much have a good idea if PHP 7.2 improves that page processing time or not. I’ll simply consider that network latency is low and is the same on both PHP versions.
These are the average results (pages not cached)
Time to First Byte – PHP 5.6
And when testing on PHP 7.2
WordPress TTFB with PHP 7.2
Of course, these tests were repeated many times. All results were very similar, showing a gain of 500ms on average on each request to a WordPress post. When requesting listing pages like the home page or categories pages, the difference was even more important.
You’re not convinced yet? You should spend half a hour wathcing this video from the PHP creator itself, when he explains what was done to make PHP7 so much better than PHP5! I hope you learn from this video as much as I did!
Should you upgrade PHP from 5.6 to 7.2 for WordPress?
Definitely! Some advantages are:
- Faster page loads, of course! The first cache miss will generate a page much faster
- Less server CPU load.
- You can serve twice as many requests using the same server
- You can save money by using a cheaper server
- You can keep the same server that will not be overloaded and serve pages faster
- PHP 7.2 is more secure
- Better compatibility with recent WordPress updates, themes and plugins
- Cached pages are also being served faster as they also run some PHP code before loading the cache from disk. Each millisecond is important!
Have you considered upgrading your server to PHP 7.2? Tell me about your results!