How to Check Your PHP Version (3 Ways)

September 19th, 2024 • 2 minutes read time

There are several ways to check which version of PHP you're running. Let's go through each one.

The terminal is the easiest way to check which PHP version you're running. This requires no code to be written, just a simple command.

php -v

You'll get an output similar to the following.

blog/7WD5jgYeUoqwMrsXoQcLwGm3Y8ZEmBaQm58Nh92P.webp

You'll notice that this gives the PHP version with (cli) appended. Crucially, this means the version of the CLI (command line interface) you're running and not necessarily the version you're running through your web server. Usually, these do match up, so it's a good indicator of the version you're actually on.

Another way to do this is by running php -i' which will dump the information about your PHP installation. Combining this with grep` means you're able to filter through this dump and find the version of PHP you're running:

php -i | grep "PHP Version"

This will give you an output similar to this:

blog/44uPwD4kmqP50W8EOSlQg7NTbzcI3Ji2DtX3f25q.webp

Because your CLI and web server can have different PHP installations (and php.ini files), checking using either phpinfo or phpversion is much more accurate.

To find out the version of PHP using phpinfo, create a PHP file somewhere that your web server can run it:

<?php

phpinfo();

Hit this in your browser, and you'll see something like this:

blog/oQogkNVtmPpAVrYiWfQwKbQpG2G0qOlXmUUmxPCx.png

As you can see, this gives you the version of PHP you're running through your web server directly at the top.

A much cleaner way to access the version of PHP you're running is to use the phpversion function.

Once again, create a PHP file somewhere that your web server can run it:

<?php

echo phpversion();

This will simply output to the page a string with the current version of PHP you're running:

8.2.16

The beauty of this function is that you can use it directly within your applications if you need to display the PHP version anywhere, like an admin panel.

Be careful when exposing the current version of your PHP installation publicly. While phpversion is less problematic, phpinfo reveals a lot about your PHP setup and should never be made public. Even publicly outputting the plain PHP version isn't advised since it reveals the exact version you're running — and any security vulnerabilities could be taken advantage of knowing the exact version you're on.

If you found this article helpful, you'll love our practical screencasts.
Author
Alex Garrett-Smith
Share :

Comments

No comments, yet. Be the first!