How to find out and check the PHP version
Short answer
The version is in the PHP_VERSION constant, and version_compare() compares it:
<?php
var_dump(version_compare(PHP_VERSION, '8.1.0', '>='));
var_dump(PHP_MAJOR_VERSION >= 8);Output:
bool(true)
bool(true)In the terminal
The php -v command shows the version, php -m the list of extensions. The terminal and the site may run different versions: on a page phpinfo() shows it.
A required extension
Check for an extension before using its functions:
<?php
foreach (['json', 'mbstring'] as $extension) {
echo $extension, ': ', extension_loaded($extension) ? 'yes' : 'no', "\n";
}Output:
json: yes
mbstring: yesFunctions used in this recipe
- version_compare — Compares two version numbers.
- extension_loaded — Checks whether a PHP extension is loaded.
- function_exists — Checks whether a function is defined.