Home  »  ArticlesGuidesHow ToProgrammingTechnology   »   How to get Active Major, Minor, and Release PHP Version in Linux

How to get Active Major, Minor, and Release PHP Version in Linux

You can find the active major, minor, and release PHP version in Linux servers with simple commands. There are two simple solutions using the PHP command-line with additional parameters.

1. Get the PHP Version Using PHP + GREP + CUT

$ php -v | grep ^PHP | cut -d' ' -f2

Will return output similar to this:

8.0.12

2. Get the PHP Version Using PHP + INTERNAL PHP_VERSION CONSTANT

$ php -r 'echo PHP_VERSION."\n";'

Will return output similar to this:

8.0.12

In the above example you can use these other PHP constants listed here:

  • PHP_MAJOR_VERSION (int)
  • PHP_MINOR_VERSION (int)
  • PHP_RELEASE_VERSION (int)
  • PHP_VERSION_ID (int)
  • PHP_EXTRA_VERSION (string)

Example: In the above examples the Major, Minor, and Release versions have been returned by default. If you only want the PHP version, without the release, you can use the following command:

$ php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION."\n";'

Which will return output similar to this:

8.0

The above examples return the Cli PHP version. Your intention may be to return the version of the Apache module or PHP-FPM and not CLI. Depending on the settings the versions may be different.

Find the PHP Version Used When Using Apache or Nginx

We have two good ways of finding the PHP version when using a web server like Apache or Nginx.

1 Create a PHP (.php) file and add the following code:

phpinfo();

Save the file on your web directory and open it using a web browser. It will show the actual PHP version being used.

2 A Better Way.

The above example may be too verbose or overkill. A simpler solution is to use this function instead.

echo php_ini_loaded_file().PHP_EOL;

There you have it, you should be able to get the right PHP version.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.