PHP 8 compatibility check and performance tips.

PHP 7 was first released on 03 December 2015. Yes, I’m sure, it’s been a while! The PHP team encouraged upgrades to PHP 7, such as twice faster, consistent 64-bit support, removal of old and unsupported SAPIs and extensions, improved fatal error resistance, to name a few.

A few years ago, I wrote a short article 80% of the web powered by PHP. [But 90% of PHP sites run PHP5], Since then, we have made some movement on this front. PHP 7 is now accounted for more than 60% of PHP versions in use, PHP still hangs around 80% market share of server-side programming languages ​​for websites.

At the end of 2021, PHP 7.4 will lose active support, So what are your thoughts? Have you checked PHP 8 compatibility? Are you ready to upgrade? What are some special features of PHP 8? Let’s see.

Table of Contents:

PHP 8 compatibility check with these tools

There are some tools available that automate the process of checking your scripts for PHP 8 compatibility. Here are a couple that I can recommend.

For example, you run Phan with something like this:

phan --project-root-directory --progress-bar -o phan.out

PHP 8 Benchmark

PHP 7 vs PHP 8 Benchmark
image via benchmark AWS Compute Blog,

We are well aware that on PHP 7 was least 2x faster than PHP 5.6. In PHP 7 vs PHP 8, the improvements are not so drastic but based on several StandardIts more than enough to be Meaningful, Apart from speed, PHP 8.0 offers new features such as the much-awaited Just In Time (JIT) CompilerOther performance optimizations, and built-in/core JSON support, to name a few,

PHP 8 Performance Tips – php.ini config tweaks

php.ini configuration example
php.ini configuration example.

PHP Opcache

First things first, something you probably already have enabled in PHP 7: OP cache [Also read: PHP Benchmarks: OPcache vs. OPcache w/ Performance Tweaks] This can be done in the php.ini config file or in your own opcache.ini file (eg. /etc/php/fpm/conf.d/10-opcache.ini).

You can check if OPcache is enabled from the phpinfo() output, or from the command line: php -v or php -i | grep opcache.enable. After you’ve confirmed that PHP OPcache is enabled, it’s always good to check its run-time statistics to make sure everything runs smoothly (e.g. no OOM restarts). See more preloaded of OPcache.

cashtool opcache status
screenshot from cashtoolOPcache Manager, a CLI. (PHP restart after upgrade)

php opcache gui
No command line access? Use OPcache Control Panel.

Enable PHP 8 JIT

PHP JIT is implemented as part of what? OP cache, You must already have OPcache enabled via php.ini/opcache.ini:

opcache.enable=1

Next, you’ll want to add the following to enable JIT:

opcache.jit_buffer_size=100M

When enabled (remember to restart PHP), the native code of PHP files is stored in an additional area of ​​OPcache’s shared memory.

enable realpath_cache

For high traffic web servers running PHP, you can squeeze out the extra throughput by setting PHP realpath_cache_size ‘correctly’.

realpath_cache_size = 256k
realpath_cache_ttl = 300

Turn off MySQL statistics in php.ini.

Check your php.ini and make sure you have both these settings on your production server mysqlnd.collect_statistics And mysqlnd.collect_memory_statistics are disabled. It should always be disabled unless you have a specific reason to enable it. You can use the MySQL command line (eg. show status;) Also related to MySQL, depending on your script, you can set up PHP Redis and/or Memcached to ease MySQL queries.

mysqlnd.collect_statistics = Off
mysqlnd.collect_memory_statistics = Off

Optimize output_buffering

PHP’s output buffer controls how HTML is sent to the browser. when set to Off (default), HTML is sent to the browser immediately while PHP processes your script. with output buffering On, PHP saves the output buffer that would otherwise hit the web browser into a string in a buffer. You can also set a specific output_buffering size (eg. output_buffering=4096, Recommended setting for is off either 4096, depending on your application. For example, if the HTML head contains CSS or JS (not a good practice), the browser can parallelize those downloads with output_buffering enabled.

output_buffering = 4096

conclusion

We are all well aware that it is important to keep PHP up to date from the security point of view. In addition to security, fast PHP performance is important as end users are becoming less tolerant of slow websites and applications. We’ve covered the tools available for checking PHP 8 compatibility, and I’ll continue to add more. Also, PHP 8 is significantly faster than PHP 7 according to various benchmarks of the web. As such, in closing, it is time to upgrade your application to PHP 8!

Leave a Comment