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

php

Solution

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.

Problem

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]}"; ```

Original source

Related problems