The phpversion Function

The phpversion function returns a string with the current PHP version number. It can take an optional parameter - the name of the extension for which you need to get the version. If the extension is not specified, the version of PHP itself is returned.

Syntax

phpphpversion(string $extension = null);

Example

Get the current PHP version:

php<?php
  echo phpversion();
?>

Code execution result (example):

php'8.1.0'

Example

Check the version of the mysqli extension:

php<?php
  echo phpversion('mysqli');
?>

Code execution result (example):

php'8.1.0'

Example

Compare the PHP version with the minimum required one:

php<?php
  if (version_compare(phpversion(), '8.0.0', '<')) {
    echo 'PHP version 8.0.0 or higher is required';
  } else {
    echo 'The PHP version is suitable';
  }
?>

Code execution result (example):

php'The PHP version is suitable'

See Also

  • the phpinfo function,
    which outputs complete information about PHP
  • the version_compare function,
    which compares two PHP versions