Retrieve the current PHP version as an integer — Amit Merchant — A blog on PHP, JavaScript, and more

0
22

Sometimes, when working on a feature that is tightly coupled with a certain version of the language, it’s important to check which version you’re running on your server.

For instance, if you want to use Enums in PHP, it’s important that you’re running PHP 8.1 or higher.

If we want to check that, we can do that by using the PHP_VERSION_ID constant. The constant represents the current PHP version as an integer. This is especially useful when you want to do a version comparison.

Note: PHP_VERSION_ID is available as of PHP 5.2.7

So, for example, here in one of the PRs in Laravel, the author is checking whether the current version PHP 8.1 or higher… and based on that includes the enum file like so.

if (PHP_VERSION_ID >= 80100) 
    include 'Enums.php';

This is particularly handy in scenarios where you know your application may run on multiple PHP versions and enable certain functionalities for certain PHP versions.

To retrieve PHP version as a stringyou can use the PHP_VERSION constant that gives the version in the form of “major.minor.release[extra]” notation.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here