How to find php executable path

From inside a PHP program I want to know the location of the binary executing it. Perl has $^X for this purpose. Is there an equivalent in PHP?

This is so it can execute a child PHP process using itself [rather than hard code a path or assume "php" is correct].

UPDATE

  1. I'm using lighttpd + FastCGI, not Apache + mod_php. So yes, there is a PHP binary.
  2. eval/include is not a solution because I'm spawning a server which has to live on beyond the request.

Things I've tried and don't work:

  • $_SERVER['_'] looks like what I want from the command line but its actually from an environment variable set by the shell of the last executed program. When run from a web server this is the web server binary.
  • which php will not work because the PHP binary is not guaranteed to be the same one as is in the web server's PATH.

Thanks in advance.

asked Aug 13, 2009 at 19:58

SchwernSchwern

144k23 gold badges180 silver badges319 bronze badges

0

The PHP_BINDIR constant gives you the directory where the php binary is

answered Aug 13, 2009 at 20:05

LepidosteusLepidosteus

11.5k4 gold badges36 silver badges51 bronze badges

6

Yeah, $_SERVER['_'] is what you're talking about, or as near as exists. The reason you're getting a Web server binary when it's run from the web is that /usr/bin/php has nothing to do with the Web server's execution; what it's running is a separate SAPI. There's nothing from the web PHP instance to point to /usr/bin/php because there's no reason for there to be.

answered Aug 13, 2009 at 20:03

chaoschaos

120k33 gold badges300 silver badges308 bronze badges

7

The PHP_BINDIR constant is probably the easiest thing to use; the next best thing I could come up with is basically re-creating that bindir path from the extension_dir configuration setting:

$phpbin = preg_replace["@/lib[64]?/.*$@", "/bin/php", ini_get["extension_dir"]];

It has a regex in it, so it feels more like your native perl[!] but otherwise is not especially optimal.

answered Aug 15, 2009 at 12:24

Wez FurlongWez Furlong

4,37428 silver badges33 bronze badges

In PHP5.4 you can use the PHP_BINARY constant, it won't work via mod_php or similar but will via CGI etc.

For earlier versions of PHP readlink['/proc/self/exe']; will probably be fine, again it won't work via mod_php.

answered Oct 28, 2014 at 15:04

dsasdsas

1,62018 silver badges30 bronze badges

Depending on the way php is installed you CANT find the php executable. if php is running as a module for the webserver like apache module, then there is no binary you can call. you can take a look into php_info[] it lists everything. may also the path to php. within that path you can assume a php binary.

but why do you want to call a extra process? you can execute other php files by include command or eval. there is no reason to spawn a new process.

answered Aug 13, 2009 at 21:05

coding Bottcoding Bott

4,2071 gold badge26 silver badges43 bronze badges

1

what about:

But, it's unix/linux only:D

answered Aug 15, 2009 at 9:31

ariefbayuariefbayu

21.4k12 gold badges70 silver badges92 bronze badges

1

I've been looking for the php7 executable on my mac [OSX El Capitan] in order to configure and install xdebug [needed to find the right version of phpize to run]. None of the solutions I found worked for me, so I just ended out searching for it:

find / -name php -print

I knew [from phpinfo[]] that I was running php7, so I was able to infer the correct directory from the options presented by find.

answered May 7, 2016 at 13:13

WittWitt

4574 silver badges10 bronze badges

1

Where is my php EXE file?

On Windows the default path for the php. ini file is the Windows directory. If you're using the Apache webserver, php. ini is first searched in the Apaches install directory, e.g. c:\program files\apache group\apache .

Where is php executable Linux?

/usr/bin/php7. 0.

Where is php EXE file in xampp?

On a Linux or Mac OS X system, this may be available as the file /usr/bin/php. If you've installed XAMPP, you can use the program /opt/lampp/bin/php on Linux, /Applications/xampp/xamppfiles/bin/php on Mac OS X, and C:\Program Files\xampp\php\php.exe on Windows.

How set php executable path in VS code?

“how to set php path in vs code” Code Answer /* File->Preferences->settings->User settings tab->extensions->from the drop down select php->on the right pane under PHP › Validate: Executable Path select edit in settings.

Chủ Đề