Reputation: 524
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.
Upvotes: 6
Views: 5895
Reputation: 9146
Taken from an example in the docs (here and here):
if (version_compare(phpversion(), '5.5.0', '>=')) {
echo "All good";
} else {
echo "Your version of PHP is too old";
}
Test script:
<?php
echo PHP_VERSION;
echo PHP_EOL;
echo phpversion();
echo PHP_EOL;
echo version_compare(phpversion(), '5.5.0', '>=') ?: "false";
echo PHP_EOL;
echo version_compare(phpversion(), '5.6.0', '>=') ?: "false";
echo PHP_EOL;
echo version_compare(phpversion(), '5.7.0', '>=') ?: "false";
echo PHP_EOL;
echo version_compare('5.4.6.1ububtu1.8', '5.4.0', '>=') ?: "false";
echo PHP_EOL;
echo version_compare('5.4.6.1ububtu1.8', '5.5.0', '>=') ?: "false";
?>
Output:
$ php test.php
5.6.4-1+deb.sury.org~trusty+1
5.6.4-1+deb.sury.org~trusty+1
1
1
false
1
false
Upvotes: 7
Reputation: 2924
You could use PHP's variable mangling capabilities to check for a specific version:
<?php
$v=PHP_VERSION;
echo "RAW: ".$v.PHP_EOL;
$n=$v+0;
echo "MANGLED: ".$n.PHP_EOL;
echo "CONSTANT DIRECTLY MANGLED: ".(PHP_VERSION+0).PHP_EOL;
$u="5.4.6.1ububtu1.8";
echo "From string: ".$u.PHP_EOL;
$nu=$u+0;
echo "From mangled string: ".$nu.PHP_EOL;
Output:
$: ~/test> php -f v.php
RAW: 5.5.24
MANGLED: 5.5
CONSTANT DIRECTLY MANGLED: 5.5
From string: 5.4.6.1ububtu1.8
From mangled string: 5.4
$: ~/test>
Upvotes: 0
Reputation: 1838
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
Upvotes: 9
Reputation: 6770
If you know for sure that you are always at or above 5.2.7.
http://php.net/manual/en/reserved.constants.php#reserved.constants.core
$phpversionmajor = PHP_VERSION_MAJOR;
$phpversionmajor = PHP_VERSION_MINOR;
if($phpversionmajor >= 5 && $phpversionminor >= 5) {
echo "All good";
} else {
echo "Your version of PHP is too old";
}
Upvotes: 1
Reputation: 8168
You can do like this
echo filter_var(PHP_VERSION, FILTER_SANITIZE_NUMBER_FLOAT,
FILTER_FLAG_ALLOW_FRACTION);
The second Parameter will sanitize the results according to a FLOAT
representation and third parameter allows fractions like a 'DOT'
Yo would want to take a loot at the PHP Manual- Sanitizing Filers
Upvotes: 0
Reputation: 433
PHP provides a convenient function, version_compare
, for comparing SemVer-like version numbers. While you could do a whole bunch of manual string wrangling, I suspect in your use case you can make your life much easier by taking advantage of what comes in the box.
Upvotes: 4
Reputation: 3743
I faced the same issue some days ago, I found this snippet from PHP Docs
// PHP_VERSION_ID is available as of PHP 5.2.7, if our
// version is lower than that, then emulate it
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
Now, PHP_VERSION_ID
constant contains, what you want.
Link here
Upvotes: 1