PHP phpversion(): Check the running PHP or extension version

phpversion() returns the version string of the PHP runtime executing the code. Use echo to display it, or pass an extension name such as phpversion('json') to check that extension’s version. To identify the version a website uses, check the runtime that handles its web requests.

Read and display the current PHP version

Save the following as version.php and run php version.php. It prints one line such as 8.3.32. The actual number depends on the runtime. The PHP_VERSION constant gives the same string. PHP Manual — phpversion()

<?php
echo phpversion(), PHP_EOL;

For a web example, save the next snippet as version-check.php. From that directory on a development computer, run php -S 127.0.0.1:8000 -t .. Visit http://127.0.0.1:8000/version-check.php and stop the server with Ctrl+C when done.

<?php
$version = phpversion();
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>PHP version check</title>
</head>
<body>
  <h1>PHP version for this request</h1>
  <p>PHP: <?= htmlspecialchars($version, ENT_QUOTES, 'UTF-8') ?></p>
  <p>Server API: <?= htmlspecialchars(PHP_SAPI, ENT_QUOTES, 'UTF-8') ?></p>
</body>
</html>

The page shows the version and the Server API, or SAPI, which identifies the execution interface. PHP’s built-in server shows cli-server, while running the same file from a shell shows cli. Opening the file directly through a file: URL does not execute PHP.

Check an extension’s version and load status

Save the next example as extension-version.php and run php extension-version.php. The two lines show the load status and version separately. Start with json, which is built into PHP 8, then change the name to an extension you use and compare the results.

<?php
$name = 'json';
$loaded = extension_loaded($name);
$version = phpversion($name);

echo 'Loaded: ', $loaded ? 'yes' : 'no', PHP_EOL;
echo 'Version: ', $version === false ? 'no version reported' : $version, PHP_EOL;

A false result means the extension is unloaded or does not report a version. Use === false to distinguish this from a version string, and use extension_loaded() to find out whether the current runtime loaded the extension. Check the extension, database server, and external library versions separately. PHP Manual — extension_loaded()

Compare a minimum version accurately

Use version_compare() to test a minimum. Converting to a decimal can lose the patch number, while plain string sorting can put 8.10.0 before 8.9.0. Save the example as version-compare.php and run php version-compare.php.

<?php
$minimum = '8.2.0';
if (version_compare(PHP_VERSION, $minimum, '>=')) {
    echo 'Minimum version met.', PHP_EOL;
} else {
    echo 'Below the minimum version.', PHP_EOL;
}

var_dump(version_compare('8.10.0', '8.9.0', '>')); // true
var_dump(version_compare('8.2.0RC1', '8.2.0', '>=')); // false

The minimum in the example is for illustration. PHP 8.2.0 or higher prints the first message. An older runtime prints the second. The two additional comparisons print bool(true) and bool(false). Version 8.10.0 sorts above 8.9.0, and 8.2.0RC1 sorts below the final 8.2.0 release. PHP Manual — version_compare()

For a condition based only on major, minor, and patch numbers, you can use the integer PHP_VERSION_ID. It omits prerelease suffixes, so use version_compare() to distinguish a release candidate from a final release. Check for a particular function or extension with function_exists() or extension_loaded() when availability is what matters. PHP Manual — Predefined constants

Resolve differences between the web and command line

php -v describes the PHP executable launched by the shell. The website may use a different executable or PHP-FPM pool. After a version change, check a request actually served by the site and compare its version and SAPI. For configuration files and loaded modules, see the phpinfo() runtime guide. Remove the version-check file when done.