Retrieve the current PHP version as an integer
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 string, you can use the PHP_VERSION
constant that gives the version in form of “major.minor.release[extra]” notation.
Like this article?
Buy me a coffee👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.