After you install PHP or LAMP on a Linux server ( or XAMP on a Windows Server ), if you want to run command php in a terminal to execute a .php script file, you should first find the PHP install path and add the php executable file path in system environment variable PATH‘s value.
But if there are multiple PHP version installed on the server, you should find the PHP version and related PHP install path which you need, and then you can run it accordingly. For example, you can invoke your required PHP version executable file on the Linux Cron job.
This article will tell you how to check current PHP version and PHP install path in both Linux and Windows. It will also tell you how to change current PHP version to another one by edit the system environment variable PATH‘s value.
1. Check PHP Install Path On Linux.
whereis command returns the executable file path. From below example, we can see the PHP executable file path is
/usr/bin/php, and it is linked to
/www/server/php/73/bin/php file ( this is the real PHP executable file ).
$ whereis php php: /usr/bin/php $ $ ls -l /usr/bin/php lrwxrwxrwx. 1 root root 26 Dec 21 09:08 /usr/bin/php -> /www/server/php/73/bin/php
whereis command returns multiple PHP install path, then you can run
which command to get current PHP executable file path.
$ whereis php php: /usr/bin/php /usr/local/bin/php /usr/local/lib/php.ini $ $ which php /usr/local/bin/php
2. Check PHP Install Path On Windows.
It is very easy for you to check PHP install path on Windows, because install PHP on Windows is just download the PHP zip file and unzip it to a local folder, then you can run it in a dos window like below. In below example, the php install path is C:\xampp\php\.
C:\WorkSpace>C:\xampp\php\php -v PHP 8.0.0 (cli) (built: Nov 24 2020 22:02:57) ( ZTS Visual C++ 2019 x64 ) Copyright (c) The PHP Group Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
If you want to run above example just by input command php -v, then you need to add the PHP install path ( C:\xampp\php\ ) in Windows system environment variable PATH‘s value. You can read article How To Set Windows Environment Variables.
# First make sure php install path has been added in windows environment variable PATH's value. C:\WorkSpace>echo %PATH% ..........;C:\xampp\php # Now you can run command php in command console. C:\WorkSpace>php -v PHP 8.0.0 (cli) (built: Nov 24 2020 22:02:57) ( ZTS Visual C++ 2019 x64 ) Copyright (c) The PHP Group Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
3. Check Current PHP Version.
php -v command in a terminal to get the current executed PHP version.
# php -v PHP 7.9.9. (cli) (built: Dec 21 2020 09:06:30) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.25, Copyright (c) 1998-2018 Zend Technologies
3. Use phpinfo() Function To Get PHP Version & PHP Install Path.
phpinfo()function can return a lot of useful information ( includes PHP Version and Install Path ) about currently used PHP.
- We can write a
.phpscript file and contain the
phpinfo()function in this file. Then we can execute it both in command-line or from HTTP web server.
- Open a terminal, run command
vi test.phpto create a .php script file.
ikey to enter insert mode.
- Copy below source code into the test.php file.
<?php phpinfo(); ?>
returnkey to save the edited file and exit.
php ./test.phpin command line. You can get below output messages.
$ php ./test.php phpinfo() PHP Version => 7.3.11 ...... ......