3 Ways to Check PHP Version

Updated: January 9, 2024 By: Guest Contributor Post a comment

Introduction

PHP is a popular server-side scripting language widely used for web development. Knowing which version of PHP you’re running is crucial, especially when you want to update your applications or configure environments. We’ll explore several ways to determine the PHP version that your environment is running.

Solution 1: phpinfo() Function

The phpinfo() function is a built-in PHP function that outputs a large amount of information about the current state of PHP, including the version.

  1. Open a text editor and create a new PHP file.
  2. Insert the phpinfo() command in the file.
  3. Save the file with a ‘.php’ extension and upload it to your web server.
  4. Navigate to the file in your web browser to view the PHP version and other details.

Example:

<?php
phpinfo();
?>

This method gives a detailed output, but it could expose sensitive server information if left unprotected. It is typically more useful during development rather than in a production environment.

Solution 2: Command Line Interface (CLI)

Using the command line interface (CLI) to check PHP version is a fast and straightforward method.

  • Open the terminal or command prompt.
  • Type the command php -v and press enter.

The command:

php -v

This method only provides the PHP version and does not expose any sensitive information, making it safer for production environments.

Solution 3: Creating a Custom PHP Script

You can write a simple PHP script that outputs the version number without the extra information provided by phpinfo().

  • Create a PHP file using a text editor.
  • Add the code snippet to display just the PHP version.
  • Upload the script to your server and run it.

Example:

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

This method gives you control over what information is shared, reducing the risk of exposing sensitive details.

Conclusion

Checking the PHP version is vital for developers and system administrators. The methods described offer flexibility depending on the level of detail required and the environment in question. For rapid checks, the CLI method is effective and secure. For detailed information, the phpinfo() function is the most comprehensive option, whereas a custom PHP script can minimize security risks while still providing essential version information. Always consider the balance between information provided and security when choosing a method for checking your PHP version.