How to check PHP version in cmd

Newer PHP versions come with more features and improvements while deprecating some obsolete features. Old PHP codes might not work with more recent PHP versions and vice versa. It is, therefore, essential to use the correct PHP version for your code.

How to check PHP version in cmd

You can check the PHP version on your system by running the php command from the command line. There are also some PHP functions that you can use in your code to get the PHP version during runtime.

php binary is available in default PATH variable for Ubuntu and other Linux variance if installed using the default package manager. You might need to use full path for the binary if it's manually installed or using other methods such as XAMPP for Windows.

Steps to check installed PHP version:

  1. Run php -v from the command line.

    $ php -v
    PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

  2. Run php -i from the command line.

    $ php -i | grep "PHP Version"
    PHP Version => 7.4.3
    PHP Version => 7.4.3

  3. Print PHP_VERSION_ID from PHP script.

  4. Print phpversion() output from PHP script.

    phpversion();
    	//Sample output: 7.4.3
    ?>

  5. View from phpinfo() output.

    phpinfo();
    ?>

    How to check PHP version in cmd

  6. Query your package manager where the PHP packages are installed from.

    $ apt show php
    Package: php
    Version: 2:7.4+75
    Priority: optional
    Section: php
    Source: php-defaults (75)
    Origin: Ubuntu
    Maintainer: Ubuntu Developers 
    Original-Maintainer: Debian PHP Maintainers 
    Bugs: https://bugs.launchpad.net/ubuntu/+filebug
    Installed-Size: 13.3 kB
    Depends: php7.4
    Download-Size: 2,712 B
    APT-Sources: http://jp.archive.ubuntu.com/ubuntu focal/main amd64 Packages
    Description: server-side, HTML-embedded scripting language (default)
     PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
     open source general-purpose scripting language that is especially suited
     for web development and can be embedded into HTML.
     .
     This package is a dependency package, which depends on latest stable
     PHP version (currently 7.4).

    Package manager query comand for different operating systems and distributions:

    PlatformCommandhomebrew$ brew list --versions phpDebian, Ubuntu$ apt show phpCentOS, RedHat, Fedora$ dnf info httpd

    1. Open a bash shell terminal and use the command “php –version” or “php -v” to get the version of PHP installed on the system.

    # php --version
    PHP 5.4.16 (cli) (built: Mar  7 2018 13:34:47) 
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

    # php -v
    PHP 5.4.16 (cli) (built: Mar  7 2018 13:34:47) 
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

    As you can see from both the command output above, the system has PHP 5.4.16 installed.

    2. You can also check for the package versions installed on the system to get the PHP version.
    On RedHat Based Distributions :

    # rpm -qa | grep php
    php-common-5.4.16-43.el7_4.1.x86_64
    php-pdo-5.4.16-43.el7_4.1.x86_64
    php-mysql-5.4.16-43.el7_4.1.x86_64
    php-mbstring-5.4.16-43.el7_4.1.x86_64
    php-cli-5.4.16-43.el7_4.1.x86_64
    php-gd-5.4.16-43.el7_4.1.x86_64
    php-5.4.16-43.el7_4.1.x86_64

    On debian based distributions :

    # apt list --installed | grep php

    3. Let’s create a PHP file with content as shown below. The phpinfo() function outputs a great deal of information about the state of the current PHP environment, including loaded extensions, compilation options, version, server information, and so on.

    # echo "<?php phpinfo();?>" > /var/www/html/phpinfo.php

    4. Now, considering that Apache is installed and working, open a browser and test PHP using address as follow:

    To find the version of PHP running on your web server, create and upload a script called "version.php" that contains some brief code, then access that script in your web browser.

    Did this summary help you?YesNo

    Thanks to all authors for creating a page that has been read 913,001 times.

    Welcome to a quick tutorial on how to check the PHP version. Yep, this is probably one of the “funky” tutorials that I have written. The PHP version is normally one that people will not touch on, but be it for checking for compatibility or if it is time for an update.

    There are 4 possible ways to do a version check on your current PHP installation:

    1. Run php -v in the command line.
    2. If you need in-depth information on your installation, create a single line script – phpinfo();
    3. Or use the version constant – echo PHP_VERSION;
    4. Finally, the PHP version function – echo phpversion();

    That should cover all the basics, but read on if you need more details.

    ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

     

     

    TLDR – QUICK SLIDES

    Download & Notes

     

    DOWNLOAD & NOTES

    First, here is the download link to the example code as promised.

     

    QUICK NOTES

    If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

     

    EXAMPLE CODE DOWNLOAD

    Click here to download the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

     

     

    PHP VERSION CHECK

    All right, let us now get into the various to check the PHP version in this section.

     

    1) COMMAND LINE PHP VERSION CHECK

    1-command.txt

    D:\>php -v
    PHP 8.0.8 (cli) (built: Jun 29 2021 16:02:52) ( ZTS Visual C++ 2019 x64 )
    Copyright (c) The PHP Group
    Zend Engine v4.0.8, Copyright (c) Zend Technologies

    Windows users, launch the command prompt and fire up php -v. Mac and Linux users, that is pretty much the same, except we call it terminal instead of command prompt.

     

    2) PHP INFO

    2-phpinfo.php

    <?php
    phpinfo();

    That’s all for this tutorial, and here is a small section on some extra links that may be useful to you.

     

    SEMANTIC VERSIONING

    So, just what the heck is with the

    <?php
    phpinfo();
    8 way of versioning? That is called “semantic versioning”, and there is a good reason behind it.

    • <?php
      phpinfo();
      9 is the major version. This number goes up only when there are incompatible or irreversible changes.
    • <?php
      // FULL PHP VERSION STRING IN FORMAT OF -
      // MAJOR.MINOR.RELEASE[EXTRA]
      echo "Full - " . PHP_VERSION . "<br>";
      echo "Major - " . PHP_MAJOR_VERSION . "<br>";
      echo "Minor - " . PHP_MINOR_VERSION . "<br>";
      echo "Release - " . PHP_RELEASE_VERSION . "<br>";
      echo "Extra - " . PHP_EXTRA_VERSION . "<br>";
      echo "Version ID - " . PHP_VERSION_ID . "<br>";
      0 is the minor version. When feature changes are made, but still backward compatible.
    • <?php
      // FULL PHP VERSION STRING IN FORMAT OF -
      // MAJOR.MINOR.RELEASE[EXTRA]
      echo "Full - " . PHP_VERSION . "<br>";
      echo "Major - " . PHP_MAJOR_VERSION . "<br>";
      echo "Minor - " . PHP_MINOR_VERSION . "<br>";
      echo "Release - " . PHP_RELEASE_VERSION . "<br>";
      echo "Extra - " . PHP_EXTRA_VERSION . "<br>";
      echo "Version ID - " . PHP_VERSION_ID . "<br>";
      1 refers to the patch version. Something like “nothing has changed, just a bug fix”.

     

    • Official PHP manual – version function.
    • Reserved constants in PHP.
    • Semantic Versioning

     

    TUTORIAL VIDEO

     

    INFOGRAPHIC CHEATSHEET

    Thank you for reading, and we have come to the end of this short guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

    How do I check my PHP version?

    Another way to check PHP version is PHPinfo() function, commonly used to check the current state of PHP configuration. It can also be used for debugging purposes as it contains all EGPCS (Environment, GET, POST, Cookie, Server) data. phpinfo();

    How to update PHP version in CMD?

    Run the brew upgrade command from the terminal as shown below:.
    brew upgrade php..
    php -v..
    sudo apt-get update sudo apt-get install php..

    What version of PHP do I have terminal windows?

    Show activity on this post..
    First open your cmd..
    Then go to php folder directory, Suppose your php folder is in xampp folder on your c drive. Your command would then be: cd c:\xampp\php..
    After that, check your version: php -v..

    What are the current version of PHP?

    PHP 8.1 is the latest version of PHP, a popular programming language used for web development and server-side scripting. It was released on December 8, 2021 and includes a number of new features and improvements over previous versions.