How to determine path to php.exe on Windows – search default paths

Question:

I’m currently developing a couple of plugins for Sublime Text 2 on OS X and I would like to make them cross platform, meaning I have to find out if and where php.exe is installed.

Right now I call /usr/bin/php in Python, which obviously works only on OS X and Linux:

phppath = '/usr/bin/php'<br>
pluginpath = sublime.packages_path() + '/HtmlTidy/tidy.php'<br>
retval = os.system( '%s "%s"' % ( phppath, scriptpath ) )

But on Windows, there seems to be no definitive default path for php.exe. The more I googled for it, the more possibilities showed up. So far I think I would have to check each of the following paths for existence:

c:phpphp.exe
c:php5php.exe
c:windowsphp.exe
c:program filesphpphp.exe
c:wampbinphpphp5php.exe
c:xamppphpphp.exe

That’s already quite a collection, but what I’m asking for is either a complete list covering all possibilities – or another way to figure it out that should be as robust as checking each possible path.

So if you have php.exe installed in some place other than these, please leave a comment with your path and I will add it to the list above.

Besides, there seems to be php.exe and php-cli.exe. I guess it would be OK to loop through each possible path. Check first for php-cli.exe, check for php.exe, and take the first match. Is that correct or is there a better practice?

Asked By: Matt

||

Answers:

If the user has added PHP’s bin folder to the system PATH, then you should just be able to try and execute php -v to check that it’s present.

If you want to obtain the full path to the PHP executable and the target system is Windows Server 2003 or later (so Windows Vista, and Windows 7) then you could use the WHERE command, i.e.:

C:> where php.exe
C:Program Files (x86)WAMPbinphpphp5.3.5php.exe

Also see possibly related question: Is there an equivalent of 'which' on the Windows command line?.

If you are really desperate to find any file on the user’s computer, you could try executing the equivalent of a find – but it’s going to be slooow!

C: && cd  && dir /s /b php.exe
Answered By: JonnyReeves

From PHP 5.4 and later, you can use the PHP_BINARY constant.

Answered By: Pierre

Try

echo PHP_BINDIR;

It works. I tested it on macOS as with PHP 5.6.

Answered By: Yevgeniy Afanasyev

Use:

C:xamppphpphp.exe

It should work if your XAMPP instance is on D: or E:. You change it accordingly from C:.

Answered By: joash

On powershell or commad prompt
php -r "echo PHP_VERSION;" gives the version

See other PHP constantas:
https://www.php.net/manual/es/reserved.constants.php

to answer your question

php -r "echo PHP_BINARY;" gives the full path to php.exe

if needed, to remove the php.exe

php -r "echo str_replace(‘php.exe’, ”, PHP_BINARY);"

Answered By: VPDD

I found a way for you to find out where the php.exe is stored. In C:, click the search bar and search for "php.exe". Then, there will appear an application called php and click it. Then, cmd will open and in the cmd tab it will say php.exe at the end. That’s where your php.exe is and you can put the link address of the file in VS Code.

Answered By: Vani