Using the correct PHP version is important to ensure compatibility with your code. Newer versions of PHP offer additional features, but older code might not work correctly with them. It's necessary to check your system’s PHP version before making any changes or updates.

There are several ways to check the PHP version on your system. You can use the command line or execute specific PHP functions in your code. These methods help you confirm that your environment meets the requirements of your project.

Verifying the PHP version is also important when managing server environments. Different operating systems, like Ubuntu, have different commands for checking installed packages. Being familiar with these commands ensures that you can maintain and troubleshoot your PHP setup effectively.

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.
    <?php 
    	echo PHP_VERSION_ID;
    	//Sample output: 70403
    ?>
  4. Print phpversion() output from PHP script.
    <?php 
    	echo phpversion();
    	//Sample output: 7.4.3
    ?>
  5. View from phpinfo() output.
    <?php 
    	phpinfo();
    ?>

  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 <ubuntu-devel-discuss@lists.ubuntu.com>
    Original-Maintainer: Debian PHP Maintainers <team+pkg-php@tracker.debian.org>
    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:

    Platform Command
    homebrew $ brew list --versions php
    Debian, Ubuntu $ apt show php
    CentOS, RedHat, Fedora$ dnf info httpd
Discuss the article:

Comment anonymously. Login not required.