During PHP development, sometimes we need to confirm the version number of a PHP extension to ensure that the environment meets the requirements. PHP built-in function phpversion() can not only query the current version number of PHP, but also use it to query the version number of the specified extension. This article will introduce in detail how to use the phpversion() function to obtain the version information of the specified PHP extension.
phpversion() is a built-in function of PHP, which returns the version number of the current PHP interpreter. When passing in parameters, the version number of the specified extension can be returned. For example:
echo phpversion(); // OutputPHPMain version number,For example7.4.10
If you need to query the version number of an extension, you can pass the extension as a parameter:
echo phpversion('curl'); // OutputcurlExtended version number
Sample code:
<?php
$extension = 'curl';
$version = phpversion($extension);
if ($version === false) {
echo "Extended '{$extension}' Not installed or not enabled。";
} else {
echo "Extended '{$extension}' The version number is:{$version}";
}
?>
This code will determine whether the extension exists and output the corresponding version number or prompt that it is not installed.
In a web environment, you can use phpversion() to output multiple extension version numbers for easy viewing by administrators:
<?php
$extensions = ['curl', 'mbstring', 'openssl'];
foreach ($extensions as $ext) {
$ver = phpversion($ext);
if ($ver) {
echo "Extended {$ext} Version:{$ver}<br>";
} else {
echo "Extended {$ext} Not installed or not enabled<br>";
}
}
?>
If you want to know more about the usage of phpversion() , you can access the official document (replace the domain name with gitbox.net):
$url = "https://gitbox.net/manual/zh/function.phpversion.php";
echo "For more details, please refer to: " . $url;
phpversion() returns the PHP main version number when there is no parameter.
Returns the version number of the specified extension when passing in the extension parameter.
Return false if the extension does not exist or is not enabled.
Suitable for dynamic detection environments in scripts to ensure that dependency extensions meet version requirements.
Mastering the phpversion() function allows you to more flexibly manage and detect PHP environments and ensure stable project operation.