How to Check PHP Version

January 14, 2021

Introduction

Hosting providers are traditionally slow to adopt new PHP versions on their servers. The consequence of this is that many different PHP versions exist on the web at the same time.

If you are implementing new features, installing a new PHP-based app, or trying to locate a bug on your website, it is important to know which PHP version your web server is running.

In this tutorial, you will learn how to check your PHP version by running PHP code on your server or using the command line.

How to Check PHP Version

Prerequisites

Check PHP Version by Running PHP Code

The simplest method to determine the PHP version running on your website is executing a PHP file that contains the following code:

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

Create the file using a text editor like gedit or Notepad, and upload it to your website’s document root directory.

Then open a web browser and type the full address of the file in the address bar. For example, if you uploaded a file titled phpinfo.php to the example.com root directory, you would go to:

http://www.example.com/phpinfo.php

The code above displays the PHP version without any further details, like in the output below:

Checking PHP version by using the phpversion command

If you need more details on your PHP configuration, such as system information, build date, server API, configuration file information, etc., upload a file containing the phpinfo() function:

<?php 
phpinfo();

When visited in the browser, this file shows the PHP version in the upper-left corner, followed by configuration data:

Checking PHP version by using the phpinfo command

Note: While phpinfo() is useful for debugging, the page features sensitive information about your system. Remove the file from the server once you finish using it.

For a list containing all the loaded PHP extensions and their versions, upload a file with the following code:

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

The output shows each extension in a separate line, including the version of the PHP core:

Checking versions of loaded PHP extensions

Check PHP Version Using the Command Line (Windows, Linux and macOS)

If you have permission to SSH into the remote server, use the command line to check the installed PHP version. This method is also useful for checking the PHP version installed locally.

1. Type the PHP command:

php -v

2. The php -v command works on Linux, macOS, Windows, and other supported systems. Its output contains the PHP version number, build date, and copyright information.

Checking PHP version by using the command line in Ubuntu

Note: If there is more than one PHP version installed on the server, the php -v command shows the default command-line interface (CLI) version. This version is not necessarily the one that runs on the hosted websites.

Fix ‘PHP is not Recognized’ Error on Windows

On Windows, the PHP path is sometimes not recognized by the system, so the php -v command outputs the ‘php is not recognized’ error.

The 'php is not recognized' error

To solve this problem, set the PATH environment variable first.

1. Type the following command, replacing [location] with the path to your PHP installation.

set PATH=%PATH%;[location]
Setting the PATH variable for php.exe in Windows

2. Typing php -v now shows the PHP version installed on your Windows system.

Note: Check out our other PHP guides such as How to Make a Redirect in PHP or 4 Different Types of Errors in PHP.

Conclusion

This article aimed to explain the common ways to check the PHP version on your server or local machine. The methods covered in this tutorial include running PHP code and using the command-line interface.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How To Install and Use PHP Composer on Ubuntu
August 29, 2019

Use Composer to specify a set of libraries for a specific project...
Read more
4 Different Types of Errors in PHP
August 6, 2019

A PHP Error occurs when something is wrong in the PHP code. An error can be simple as a missing semicolon...
Read more
PHP Error Reporting: How to Enable & Display All Errors / Warnings
August 6, 2019

If a script is not written correctly, or if something unusual happens, PHP can generate an error message....
Read more
How To Install PHP 7, 7.2 & 7.3 On CentOS 7
May 24, 2019

PHP is a programming language that's often used to automate server tasks. It's part of the LAMP...
Read more