25

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:\php\php.exe
c:\php5\php.exe
c:\windows\php.exe
c:\program files\php\php.exe
c:\wamp\bin\php\php5\php.exe
c:\xampp\php\php.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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Matt
  • 385
  • 1
  • 4
  • 6
  • 3
    I would just search for it in the Windows PATH, and then if it's not there, prompt the user for it. Your list can never be complete. And, what if the user has multiple paths to a php.exe? I have 5 or 6 php.exe right now. – Corbin Mar 10 '12 at 10:02
  • 3
    Take a look for the `PHPRC` environment variable. It contains the directory where `php.exe` is located. It's often available on windows systems. – hakre Mar 10 '12 at 10:10
  • you might be able to search the registry, but I'm no expert on it so I ccouldn't say how. – aquavitae Mar 10 '12 at 10:10

5 Answers5

43

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)\WAMP\bin\php\php5.3.5\php.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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
JonnyReeves
  • 5,919
  • 1
  • 23
  • 28
  • `where` doesn't find my `php.exe`. Indeed it seems only to look in PATH and the current directory. – Kos Mar 10 '12 at 10:54
  • @Kos could you tell us, where *your* php.exe actually is? – Matt Mar 10 '12 at 11:54
  • Thank you JonnyReeves, `where` would probably do the job, but the first answer on the link you posted led me to this python script which seems to deal quite well with PATH and PATHEXT: http://nedbatchelder.com/code/utilities/wh_py.html - since the plugin for Sublime Text 2 is in python, I'm going to use some lines of code from it to minimize calling external programs. – Matt Mar 10 '12 at 12:55
  • @Mat a regular WAMP installation like mentioned before, `C:\wamp\bin\php\php5.3.9\php.exe` – Kos Mar 11 '12 at 11:14
  • `where /?` says "By default, the search is done along the current directory and in the paths specified by the PATH environment variable." – Kos Mar 11 '12 at 11:15
  • in windows, you can check your path if it has php or not with: `$env:Path.Replace(';', "\`n") | findstr "php"` – kia nasirzadeh Oct 12 '20 at 23:54
6

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

Pierre
  • 716
  • 4
  • 9
2

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);"

Dharman
  • 21,838
  • 18
  • 57
  • 107
VPDD
  • 71
  • 3
  • 5
0

Use:

C:\xampp\php\php.exe

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
joash
  • 1,409
  • 1
  • 18
  • 24
-1

Try

echo PHP_BINDIR;

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Yevgeniy Afanasyev
  • 27,544
  • 16
  • 134
  • 147