How to get the PHP Version?

Viewed 103374

Is there a way to check the version of PHP that executed a particular script from within that script? So for example, the following snippet

$version = way_to_get_version();
print $version;

would print 5.3.0 on one machine, and 5.3.1 on another machine.

11 Answers
$version = phpversion();
print $version;

Documentation

However, for best practice, I would use the constant PHP_VERSION. No function overhead, and cleaner IMO.

Also, be sure to use version_compare() if you are comparing PHP versions for compatibility.

Technically the best way to do it is with the constant PHP_VERSION as it requires no function call and the overhead that comes with it.

echo PHP_VERSION;

constants are always faster then function calls.

You can either use the phpversion() function or the PHP_VERSION constant.

To compare versions you should always rely on version_compare().

.........

if (version_compare(phpversion(), '5', '>='))
{
       // act accordintly
}

Take a look at phpversion().

echo "Current version is PHP " . phpversion();

phpversion() will tell you the currently running PHP version.

you can use phpversion() function to get php version

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

You can use phpversion(); function to find the current version

    <?php echo 'Current PHP version: ' . phpversion(); ?>

phpversion() is one way. As John conde said, PHP_VERSION is another (that I didn't know about 'till now).

You may also be interested in function_exists()

Related