How to check PHP version

Posted on Jul 20, 2022

Learn how to check PHP version installed on your server or computer

Photo from Unsplash

PHP has seen steady development since the release of PHP 7. Each PHP version is supported for 3 years from its official release date.

Sometimes, a new version may add a new feature not available in the previous versions. This is why you need to check the PHP version you have to see if it’s supported by your application.

Knowing the PHP version also helps you in debugging an issue found on your website.

This tutorial will help you learn how to check your PHP version.

Check PHP version using the command line

If you have access to the command line or terminal, then you can check your php version by running the php -v or php --version command.

Here’s an example of the output:

$ php -v
PHP 8.1.5 (cli) (built: Apr 16 2022 00:03:58) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.5, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.5, Copyright (c), by Zend Technologies

You can see that the command shows the PHP version as PHP 8.1.5.

But sometimes you may not have access to the command line, or you may install PHP using a local stack like XAMPP or WAMP.

You need to use the next method when this is the case.

Check PHP version using phpinfo()

If you can’t check the PHP version from the command line, then you can call the phpinfo() function from a PHP page.

Let’s create a new file named phpinfo.php with the following code:

<?php
phpinfo();

Save the file, then open it from the browser.

You should see a page containing PHP information rendered as shown below:

In the output, you can see the PHP version, the extensions installed, and also the configurations of your PHP engine.

Check PHP version using phpversion()

Alternatively, you can also call the phpversion() function to see just the PHP version:

<?php
echo "PHP version: " . phpversion();

The output will be a text of the PHP version like this:

The phpversion() function can also be used to check the PHP core and extensions version.

You need to call the get_loaded_extensions() to get an array of extensions installed on your PHP engine.

Then loop over the array using foreach() and echo the extensions by calling phpversion() as shown below:

<?php
$extensions = get_loaded_extensions();
foreach ($extensions as $ext) {
    echo $ext . " => " . phpversion($ext) . "<br/>";
}

The code above will render the following output:

The more extensions installed on your PHP engine, the longer the list above gets.

Conclusion

Now you’ve learned how to check the PHP engine version installed on your computer in three different ways.

You’ve also learned how to check the version of extensions installed on your PHP engine. This is useful when you are debugging an application that has an issue with one of these extensions.

To get the most complete information on your PHP engine, use the phpinfo() function.

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.