Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

validation

php

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';
}
like image 970
Kalle H. Väravas Avatar asked Aug 25 '11 21:08

Kalle H. Väravas


People also ask

How do I find the PHP version on a server?

Check PHP Version by Running PHP Code The simplest method to determine the PHP version running on your website is executing a PHP file that contains the following code: <? php echo 'PHP version: ' . phpversion();

What version of PHP should I use 2022?

WordPress 6.1, released on November 1, 2022, has finally announced full support for PHP 8.0 and 8.1. PHP 8.1 is the latest stable PHP version in the phase of active community support. WordPress website owners are encouraged to upgrade to PHP 8.1 to leverage new features and ensure further security coverage.

Is PHP 5.6 still supported?

No. The developers of PHP are no longer supporting PHP 5.6. There will not be any more security updates to PHP 5.6, there will not be any more bug fixes to PHP 5.6. You should not use PHP 5.6 (or any version of PHP 5) in a production environment.

How do you know which PHP version I am using in WordPress?

Well, if you're running WordPress 5.0 or higher then you can simply check the PHP version on the WordPress website. Just login to your Admin Dashboard then click on Tools > Site Health. Now click on Server there you will find a section called PHP Version.


2 Answers

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.

like image 133
Devin M Avatar answered Sep 29 '22 18:09

Devin M


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

like image 27
Luwe Avatar answered Sep 29 '22 20:09

Luwe