开发者

How to simply check if servers PHP version is 5 or above?

开发者 https://www.devze.com 2023-03-31 08:35 出处:网络
I\'m creating a pre-installation checklist for a program. The program requires PHP5, so I need the checklist-script to开发者_C百科 check for PHP5\'s availability.

I'm creating a pre-installation checklist for a program. The program requires PHP5, so I need the checklist-script to开发者_C百科 check for PHP5's availability.

There is a function as phpversion(), that will return in the format of 5.3.6 or similar. However, I want the checklist to be very straight forward and simply tell you yes or no. So displaying the current version isn't helping me that much. Okay, one way is to use the phpversion() and remove the comas etc. But isn't there a neater way? (Weirdly enough, there is no information on this anywhere)

So, How to simply check if servers PHP version is 5 or above?

if (...) {
    echo 'Server has PHP5 or above!';
} else {
    echo 'Servers PHP version is lower then PHP5';
}


Something like this you may be able to adapt:

<?php
if (version_compare(PHP_VERSION, '6.0.0') >= 0) {
    echo 'I am at least PHP version 6.0.0, my version: ' . PHP_VERSION . "\n";
}

if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
    echo 'I am at least PHP version 5.3.0, my version: ' . PHP_VERSION . "\n";
}

if (version_compare(PHP_VERSION, '5.0.0', '>=')) {
    echo 'I am using PHP 5, my version: ' . PHP_VERSION . "\n";
}

if (version_compare(PHP_VERSION, '5.0.0', '<')) {
    echo 'I am using PHP 4, my version: ' . PHP_VERSION . "\n";
}
?>

You can see the documentation here.


There is a predefined constant:

echo PHP_MAJOR_VERSION // displays 5

http://nl.php.net/manual/en/reserved.constants.php#reserved.constants.core

So:

if (defined('PHP_MAJOR_VERSION') && PHP_MAJOR_VERSION >= 5) 
{
  echo 'Server has PHP 5 or above!';
} 
else 
{
  echo 'Servers PHP version is lower then PHP5';
}

Above only works for PHP > 5.2.7, try this instead for lower versions:

if (strnatcmp(phpversion(),'5.0.0') >= 0)
{
  echo '5 or higher';
}
else
{
  echo '4 or lower';
}

It is suggested in one of the comments here: http://www.php.net/manual/en/function.phpversion.php#91816


See http://www.php.net/manual/en/function.version-compare.php for an example:

if (version_compare(PHP_VERSION, '5.0.0', '>=')) { ...


If the function phpversion() is returning it just as a straight number, you could do:

if ( phpversion() >=5 ) {
    echo 'Server has PHP5 or above!';
} else {
    echo 'Servers PHP version is lower then PHP5';
}


echo 'Current PHP version: ' . phpversion();

You can have further information in the PHP documentation. This is available in PHP4 and over.


$version = explode('.', phpversion());
$major = (int)$version[0];
$minor = (int)$version[1];
$release = (int)$version[2];


if ( substr(phpversion(),0,1) >= 5) {
  //php >= 5
}else{
  //php < 5
}

That wasn't so hard, was it?

EDIT: If we're worried about version 10 breaking the above code:

if ( strtok(phpversion(),'.') >= 5) {
    // php >= 5
}else{
    // php < 5
}


Actually, Symfony2 already includes such a script. It's in app/check.php. It checks for a minimum of PHP 5.3.2 and also a lot of other things.

I suggest you just use that.


echo PHP_VERSION_ID;
//You use version 5.3.7 the output like 50307

or echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION.PHP_RELEASE_VERSION; //You use version 5.3.7 the output like 537

0

精彩评论

暂无评论...
验证码 换一张
取 消