Kalle H. Väravas
Kalle H. Väravas

Reputation: 3615

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

I'm creating a pre-installation checklist for a program. The program requires PHP5, so I need the checklist-script to 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';
}

Upvotes: 8

Views: 17970

Answers (9)

Luwe
Luwe

Reputation: 3034

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

Upvotes: 19

user3003906
user3003906

Reputation: 1

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

Upvotes: 0

igorw
igorw

Reputation: 28259

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.

Upvotes: 0

timdev
timdev

Reputation: 62924

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
}

Upvotes: 1

GolezTrol
GolezTrol

Reputation: 116180

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

Upvotes: 1

Devin M
Devin M

Reputation: 9752

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.

Upvotes: 24

pulsar
pulsar

Reputation: 580

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

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

Upvotes: 11

katherine
katherine

Reputation: 281

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';
}

Upvotes: 4

Patrick Desjardins
Patrick Desjardins

Reputation: 140993

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

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

Upvotes: 1

Related Questions