23

How can I get the binary path of php from PHP?

I saw it in phpinfo(), but I need another method that gets it in Linux and Windows systems.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wiliam
  • 3,714
  • 7
  • 36
  • 56
  • 1
    possible duplicate of [Get current PHP executable from within script?](http://stackoverflow.com/questions/2372624/get-current-php-executable-from-within-script) – Gordon Oct 08 '10 at 10:20

9 Answers9

20

You can use:

$_SERVER['_']

Also, the predefined constant PHP_BINDIR gives the directory where the PHP executable is found.

Sample on CodePad and Ideone.

It looks like, for security reasons, $_SERVER values are not exposed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codaddict
  • 445,704
  • 82
  • 492
  • 529
18

Linux Only

Use the "which" command to find php.

$phpPath = exec("which php");

Note this does not guarantee the same php executable that your web server may be using, but rather the first instance that was found while looking through the paths.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SeanDowney
  • 17,368
  • 20
  • 81
  • 90
11

A method using environment variables, assuming the php executable is in the system path.

function getPHPExecutableFromPath() {
  $paths = explode(PATH_SEPARATOR, getenv('PATH'));
  foreach ($paths as $path) {
    // We need this for XAMPP (Windows)
    if (strstr($path, 'php.exe') && isset($_SERVER["WINDIR"]) && file_exists($path) && is_file($path)) {
        return $path;
    }
    else {
        $php_executable = $path . DIRECTORY_SEPARATOR . "php" . (isset($_SERVER["WINDIR"]) ? ".exe" : "");
        if (file_exists($php_executable) && is_file($php_executable)) {
           return $php_executable;
        }
    }
  }
  return FALSE; // Not found
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SirDarius
  • 41,440
  • 8
  • 86
  • 100
  • @ggirtsou I don't get your edit... why would the Windows Path contain an entry with "php.exe" ? If it is something specific to XAMPP please at least add a meaningful comment in the code. – SirDarius Oct 07 '12 at 19:56
  • @ggirtsou then add a meaningful comment in the code, because it is certainly not standard behaviour – SirDarius Oct 08 '12 at 10:07
  • Those file_exists() are redundant – ymakux Feb 16 '18 at 05:58
9

Maybe the best solution is in the Symfony process component:

PhpExecutableFinder.php and ExecutableFinder.php. In use:

<?php
    use Symfony\Component\Process\PhpExecutableFinder;

    $phpFinder = new PhpExecutableFinder;
    if (!$phpPath = $phpFinder->find()) {
        throw new \Exception('The php executable could not be found, add it to your PATH environment variable and try again');
    }

    return $phpPath;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

Normally, in a simple default PHP installation under Windows, the php.ini file is located and loaded from the same directory of the PHP binary.

To simplify, Windows users:

echo dirname(php_ini_loaded_file()).DIRECTORY_SEPARATOR.'php.exe';

Voilà!

Of course, if you are using multiple .ini files, it may not work if the files are not into the same PHP binary directory. BTW, this may solve to most of cases. Windows developers running PHP from local development environment.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel Omine
  • 141
  • 1
  • 4
  • sorry forgot to say I am using mac – Evgeny Fedorenko May 13 '16 at 18:25
  • Are the last two sentences (directly) connected? – Peter Mortensen Dec 16 '20 at 12:27
  • This solved it for me, thanks! But you might want to add `chr(34)` so it will be `$runCommand = chr(34) . dirname(php_ini_loaded_file()).DIRECTORY_SEPARATOR.'php.exe' . chr(34);` in order to support spaces in the path. Needed for example if like to run it in the background like in https://stackoverflow.com/a/73118719/4829915. – LWC Jul 03 '23 at 19:18
3

In Windows, using WAMP, you can use the ini variable - extension_dir - as it is placed in the PHP folder.

Like this:

echo str_replace('ext/', 'php.exe', ini_get('extension_dir'));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John
  • 158
  • 1
  • 5
  • You shouldn't correlate this logic to that setting - even in WAMP or whatever - the setting might be changed intentionally. – Christian May 21 '23 at 13:10
  • You are absolutely correct. This would not work inside any framework or such that need to be able to work in every project out there. This was just the solution that worked best for me at the time for my local and server setup. – John May 23 '23 at 07:30
2

As of PHP 5.4 you can simply use the PHP_BINARY reserved constant.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe
  • 877
  • 1
  • 11
  • 26
0

It's very easy!

var_dump(getenv('PHPBIN'));

But it works only on Windows, so we should use this answer.

How did I get this? I just typed echo echo phpinfo(); and searched the php path there. Just see here:

How I found the PHP path

Then I just getting it here: php getenv and ... you see the result.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aftamat4ik
  • 718
  • 8
  • 14
0

For Windows and XAMPP:

$php = getenv('PHPRC') . '/php.exe';

if(is_file($expected)){
   return $php;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ymakux
  • 3,415
  • 1
  • 34
  • 43