409 of 410 menu

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

phpversion(string $extension = null);

Example

Get the current PHP version:

<?php echo phpversion(); ?>

Code execution result (example):

'8.1.0'

Example

Check the version of the mysqli extension:

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

Code execution result (example):

'8.1.0'

Example

Compare the PHP version with the minimum required one:

<?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):

'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
byenru