tags:

views:

80

answers:

4

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

You can try and parse the phpinfo() result.

Itay Moav
+1  A: 

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

http://php.net/manual/en/function.phpversion.php

LeguRi
+1  A: 

Is there anything useful in $_ENV?

The SHELL environment variable on Unix has the path to the shell that's currently running. If you add #!/path/to/php to the top of your PHP file, make it executable and run the file directly, does $_ENV['SHELL'] contain /path/to/php?

Andy Shellam
`$_ENV` contains the same `_` entry that's populated into `$_SERVER`, but no, `$_ENV['SHELL']` doesn't contain the right value (`"/bin/zsh"`).
benizi
A: 

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;
    }

I'll try working on a Windows version later on.

EDIT

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().

cmptrgeekken