Encodera

Thanks for reading!

Donate to the site — we work hard for you.

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: yes

Functions used in this recipe

Thanks for reading!

Donate to the site — we work hard for you.