How to Get the PHP Version

PHP: How to check PHP version?

Use this native function: phpversion(). In my case it returns ...

php > echo phpversion();
7.4.3

If you just need to check if php version is 7 or higher ...

if ((int) phpversion() < 7) {
throw new \Error("Sorry, PHP >=7 required");
}

Get the PHP version number only

To get full version (5.4.6.1)

  <?php
preg_match("#^\d+(\.\d+)*#", PHP_VERSION, $match);
echo $match[0];

It will return 5.4.6.1 in your example

To get version as you need (5.4)

preg_match("#^\d.\d#", "5.4.6.1ububtu1.8", $match);
echo $match[0];

It will return 5.4 in your example

Test here

Bash script to get php version

If you're building your automations with apt, then you should be relying on apt and/or dpkg to get the relevant information.

dpkg -l 'php*' | grep ^ii

For completenes, yum/dnf/rpm:

rpm -qa 'php*'


Related Topics



Leave a reply



Submit