views:

91

answers:

4

On Linux $_SERVER["_"] contains path to PHP interpreter executable (for example /usr/bin/php -r 'echo $_SERVER["_"];' will print /usr/bin/php). On Windows XP with PHP 5.3 $_SERVER["_"] is NULL.

A: 

I assume you refer to running PHP in CLI mode.

I just tested this in CLI PHP 5.3 on Windows 7, and there seems in fact to be no way to find out the PHP executable the current script is running under (The $_SERVER["_"] index is not available; neither is there any other variable that contains the executable's path).

Also, the Command Line PHP on Windows page in the PHP manual has nothing to say on the issue. argv and php_sapi_name() don't reveail anything, either.

The only thing that comes to mind is a kludgy workaround of setting an environment variable before calling the script:

SET PHPEXE=C:\php\bin\php.exe
c:\php\bin\php.exe -f scriptname.php

and then in the PHP script:

$php_exe_path = $_SERVER["PHPEXE"];

Not really great, but I have no better idea....

Pekka
A: 

While not perfect, you could try this:

$_SERVER['phprc'] . 'php.exe'

which would give you something like

C:\Program Files\PHP\php.exe

Jamza
Interesting, but I am not getting this variable in CLI mode (PHP 5.3 on Windows 7) Are you sure this is not an environment variable set by server software or something?
Pekka
Odd, It was a fresh CLI mode installation that I found the PHPRC variable in. :/
Jamza
@Jamza it seems to be an environment variable: http://www.php.net/manual/en/configuration.php (in the notes) with that caveat, it may still be available in some installs (not on my xampp lite, though)
Pekka
A: 

Not a real solution to find the php.exe, but you might use the include_path or extension_dir and go to their parent directory in which the php.exe should be stored. As an example:

echo str_replace('\ext', '', ini_get('extension_dir'));
Kau-Boy
Thanks for not explaing the down vote :)
Kau-Boy
+7  A: 

That's nothing to do with PHP itself. It's shell that defines that environment variable. PHP just picks it up

For instance, see here:

The shell sets up some default shell variables; PS2 is one of them. Other useful shell variables that are set or used in the Korn shell are:

  • _ (underscore) -- When an external command is executed by the shell, this is set in the environment of the new process to the path of the executed command. In interactive use, this parameter is also set in the parent shell to the last word of the previous command.
  • ...

I think your best shot in Windows is to write an internal function. E.g.

PHP_FUNCTION(get_php_path)
{
    char path[MAX_PATH];
    int result;

    if (zend_parse_parameters_none() == FAILURE)
        return;

    result = GetModuleFileNameA(NULL, path, MAX_PATH);

    if (result == 0)
        RETURN_FALSE;

    if (result == MAX_PATH) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Path is too large");
        RETURN_FALSE;
    }

    RETURN_STRINGL(path, result, 1);
}

Example:

>php -r "echo get_php_path()";
D:\Users\Cataphract\Documents\php-trunk\Debug_TS\php.exe
Artefacto
Good explanation why it isn't there in Windows.
Pekka
Thank you. I didn't know it's feature of shell.
Jakub Kulhan