Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the PHP version number only

Tags:

php

server

I've been using this...

echo PHP_VERSION;

...to display the version of PHP running on the server, which usually displays something like...

5.4.6.1

But I've just discovered that on some servers I get more than the version number, and instead this gets displayed:

5.4.6.1ububtu1.8

Is there a way I can get just the numbers?

The reason being is I need to check the version of PHP and display a message, something like...

$phpversion = PHP_VERSION;
if($phpversion >= "5.5") {
    echo "All good";
} else {
    echo "Your version of PHP is too old";
}

...but of course if $phpversion contains anything other than numbers then this script won't work properly.

Thanks in advance.

like image 896
User_FTW Avatar asked Dec 01 '22 14:12

User_FTW


1 Answers

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

like image 111
Akrramo Avatar answered Dec 05 '22 23:12

Akrramo