Get current PHP executable from within script?
Posted
by benizi
on Stack Overflow
See other posts from Stack Overflow
or by benizi
Published on 2010-03-03T15:41:20Z
Indexed on
2010/03/19
20:31 UTC
Read the original article
Hit count: 355
I want to run a PHP cli program from within PHP cli. On some machines where this will run, both php4 and php5 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's no such variable in PHP?
Right now, I'm using $_SERVER['_']
, because bash (and zsh) set 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). Falling back to using:
$version = explode('.', phpversion());
$phpcli = "php{$version[0]}";
© Stack Overflow or respective owner