WordPress from PHP 5.6 to PHP 7.2! Better performance?

Em DevOps by Alex Benfica

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?

Leia também

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:

And also some warnings that I also fixed:

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:

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)

Benchmark WordPress - Time to First Byte on WordPress with PHP 5.6

Time to First Byte – PHP 5.6

And when testing on PHP 7.2

Benchmarking WordPress 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:

Have you considered upgrading your server to PHP 7.2? Tell me about your results!

Sobre o autor

Autor Alex Benfica

IT professional with over 20 years of experience in industry. BSc in Computational Mathematics, always learning and enthusiastic about software development and automation. Full stack developer proficient in Javascript, Node, React, Python, Docker, API design and many more!

Leave a Reply