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.
Prerequisites
- PHP installed
- Write access to the server’s file system
- Access to the command line
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:
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:
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:
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.
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.
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]
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.