benizi
benizi

Reputation: 4216

How can I get the current PHP executable from within a script?

I want to run a PHP CLI program from within the PHP CLI. On some machines where this will run, both PHP 4 and PHP 5 are installed. If I run the outer program as

php5 outer.php

I want the inner script to be run with the same PHP version. In Perl, I would use $^X to get the Perl executable. It appears there isn't any such variable in PHP.

Right now, I'm using $_SERVER['_'], because Bash (and zsh) sets the environment variable $_ to the last-run program. But, I'd rather not rely on a shell-specific idiom.

UPDATE: Version differences are but one problem. If PHP isn't in PATH, for example, or isn't the first version found in PATH, the suggestions to find the version information won't help.

Additionally, csh and variants appear to not set the $_ environment variable for their processes, so the workaround isn't applicable there.

UPDATE 2: I was using $_SERVER['_'], until I discovered that it doesn't do the right thing under xargs (which makes sense... zsh sets it to the command it ran, which is xargs, not php5, and xargs doesn't change the variable). I am falling back to using:

$version = explode('.', phpversion());
$phpcli = "php{$version[0]}";

Upvotes: 26

Views: 25549

Answers (9)

ZipXap
ZipXap

Reputation: 61

Update; nowadays PHP_BINARY works with XAMPP as well (Tested with XAMPP that comes with PHP 7.4).


Old Answer

Unfortunately PHP_BINARY is returning the httpd binary (on Windows XAMPP), so I'm back to using paths...

    if (defined('PHP_BINARY') &&
                PHP_BINARY &&
                in_array(PHP_SAPI, array('cli', 'cli-server')) &&
                is_file(PHP_BINARY)) {

          return PHP_BINARY;
    } else if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
        $paths = explode(PATH_SEPARATOR, getenv('PATH'));
        foreach ($paths as $path) {
            if (substr($path, strlen($path)-1) == DIRECTORY_SEPARATOR) {
                $path = substr($path, 0, strlen($path)-1);
            }
            if (substr($path, strlen($path) - strlen('php')) == 'php') {
                $response = $path.DIRECTORY_SEPARATOR . 'php.exe';
                if (is_file($response)) {
                    return $response;
                }
            } else if (substr($path, strlen($path) - strlen('php.exe')) == 'php.exe') {
                if (is_file($response)) {
                    return $response;
                }
            }
        }
    } else {
        $paths = explode(PATH_SEPARATOR, getenv('PATH'));
        foreach ($paths as $path) {
            if (substr($path, strlen($path)-1) == DIRECTORY_SEPARATOR) {
                $path = substr($path, strlen($path)-1);
            }
            if (substr($path, strlen($path) - strlen('php')) == 'php') {
                if (is_file($path)) {
                    return $path;
                }
                $response = $path.DIRECTORY_SEPARATOR . 'php';
                if (is_file($response)) {
                    return $response;
                }
            }
        }
    }
    return null;

Upvotes: 6

jlh
jlh

Reputation: 4717

After being dissatisfied with the answers, I came up with my own. It tries to use PHP_BINARY if it looks reasonable, otherwise it looks for an interpreter with the same version as the currently running one.

/**
 * Return a suitable PHP interpreter that is likely to be the same version as the
 * currently running interpreter.  This is similar to using the PHP_BINARY constant, but
 * it will also work from within mod_php or PHP-FPM, in which case PHP_BINARY will return
 * unusable interpreters.
 *
 * @return string
 */
public function getPhpInterpreter(): string {
    static $cachedExecutable = null;

    if ($cachedExecutable !== null) {
        return $cachedExecutable;
    }

    $basename = basename(PHP_BINARY);

    // If the binary is 'php', 'php7', 'php7.3' etc, then assume it's a usable interpreter
    if ($basename === 'php' || preg_match('/^php\d+(?:\.\d+)*$/', $basename)) {
        return PHP_BINARY;
    }

    // Otherwise, we might be running as mod_php, php-fpm, etc, where PHP_BINARY is not a
    // usable PHP interpreter.  Try to find one with the same version as the current one.

    $candidates = [
        'php' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
        'php' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION,
        'php' . PHP_MAJOR_VERSION,
    ];

    $envPath = $_SERVER['PATH'] ?? '';
    $paths = $envPath !== '' ? explode(':', $envPath) : [];

    if (!in_array(PHP_BINDIR, $paths, true)) {
        $paths[] = PHP_BINDIR;
    }

    foreach ($candidates as $candidate) {
        foreach ($paths as $path) {
            $executable = $path . DIRECTORY_SEPARATOR . $candidate;
            if (is_executable($executable)) {
                $cachedExecutable = $executable;
                return $executable;
            }
        }
    }

    // Fallback, if nothing else can be found
    $cachedExecutable = 'php';
    return $cachedExecutable;
}

Let me know if this works for you. I tested it on Debian Buster, from CLI and from FPM.

Upvotes: 0

cmptrgeekken
cmptrgeekken

Reputation: 8092

Okay, so this is ugly, but it works on Linux:

<?php
    // Returns the full path of the current PHP executable
    function get_proc_name(){
       // Gets the PID of the current executable
       $pid = posix_getpid();

       // Returns the exact path to the PHP executable.
       $exe = exec("readlink -f /proc/$pid/exe");
       return $exe;
    }

It doesn't look like there's any easy way to do this for Windows. Some Windows executables like tasklist can give you the name of the executable, but not the full path to where the executable is. I was only able to find examples for finding the full path given a PID for C++, AutoHotkey and the like. If anyone has any suggestions on where else I could look, let me know.

PS: To get the PID in PHP for Windows, you apparently have to call getmypid().

Upvotes: 3

CJSewell
CJSewell

Reputation: 183

Unfortunately I can't find a shorter way, but findPHP() works pretty well and is multi OS/PHP version compatible.

$lookIn could probably be extended to include more common locations.

Upvotes: -1

Guy Bami
Guy Bami

Reputation: 25

I think the best constant is PHP_BINARY. With PHP 5.5.12.

Upvotes: -3

Alessandro Sturniolo
Alessandro Sturniolo

Reputation: 99

On my server I've PHP 5.3.14.

I've found a predefined constant: PHP_BIN_DIR

Then, supposing the file name of the executable file is always 'php', $php_cmd = PHP_BIN_DIR.'/php' points to my PHP executable file.

Upvotes: 9

Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53606

You can try and parse the phpinfo() result.

Upvotes: -2

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

You could use phpversion() to get the current version of PHP before you execute the "inner" script.

Upvotes: 2

Kristopher Ives
Kristopher Ives

Reputation: 6045

It is worth noting that now in PHP 5.4+ you can use the predefined constant PHP_BINARY:

PHP_BINARY

Specifies the PHP binary path during script execution. Available since PHP 5.4.

Upvotes: 39

Related Questions