24

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,030
  • 21
  • 100
  • 124
Wiliam
  • 3,603
  • 7
  • 32
  • 55
  • 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

21

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,030
  • 21
  • 100
  • 124
codaddict
  • 429,241
  • 80
  • 483
  • 523
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,030
  • 21
  • 100
  • 124
SeanDowney
  • 16,768
  • 18
  • 79
  • 89
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,030
  • 21
  • 100
  • 124
SirDarius
  • 39,072
  • 8
  • 82
  • 98
  • @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,030
  • 21
  • 100
  • 124
5

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,030
  • 21
  • 100
  • 124
John
  • 144
  • 1
  • 3
3

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,030
  • 21
  • 100
  • 124
Daniel Omine
  • 131
  • 1
  • 4
1

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Joe
  • 797
  • 1
  • 12
  • 23
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,030
  • 21
  • 100
  • 124
aftamat4ik
  • 648
  • 8
  • 14
0

For Windows and XAMPP:

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

if(is_file($expected)){
   return $php;
}
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
ymakux
  • 3,207
  • 1
  • 32
  • 39