How to Check PHP Version Quickly and Easily
You can check the PHP version by running
php -v in the command line or by creating a PHP file with phpinfo() function. Both methods show the current PHP version installed on your system.Syntax
There are two main ways to check the PHP version:
- Command line: Use
php -vto display the version. - PHP script: Use
phpinfo()function inside a PHP file to show detailed PHP info including the version.
bash and php
php -v <?php phpinfo(); ?>
Output
PHP 8.2.4 (cli) (built: Apr 21 2024 12:00:00) ( NTS )
Copyright (c) The PHP Group
... (phpinfo output shows PHP version and configuration) ...
Example
This example shows how to create a PHP file that outputs the PHP version using phpinfo(). Save this code as version.php and open it in a browser.
php
<?php
// Display full PHP information including version
phpinfo();
?>Output
A web page showing detailed PHP configuration including the PHP version at the top.
Common Pitfalls
Some common mistakes when checking PHP version include:
- Running
php -vin a terminal where PHP is not installed or not in the system PATH. - Using
phpinfo()on a server without saving the file with a.phpextension or not accessing it via a web server. - Confusing the PHP version in CLI and the version used by the web server if they differ.
php
// Wrong: calling phpinfo without PHP tags phpinfo(); // Right: <?php phpinfo(); ?>
Quick Reference
| Method | Command/Code | Description |
|---|---|---|
| Command Line | php -v | Shows PHP version in terminal |
| PHP Script | Displays PHP version and config in browser | |
| PHP Script (version only) | Prints only the PHP version string |
Key Takeaways
Use
php -v in the terminal to quickly check PHP version.Create a PHP file with
phpinfo() to see detailed PHP info in a browser.Ensure PHP is installed and accessible in your system PATH for command line checks.
Remember CLI PHP version may differ from the web server PHP version.
Use
echo PHP_VERSION; to print just the version string in PHP code.