Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_SERVER['_'] equivalent on Windows

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.

like image 961
Jakub Kulhan Avatar asked Aug 29 '10 15:08

Jakub Kulhan


2 Answers

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
like image 192
Artefacto Avatar answered Sep 19 '22 08:09

Artefacto


While not perfect, you could try this:

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

which would give you something like

C:\Program Files\PHP\php.exe

like image 20
Jamescun Avatar answered Sep 20 '22 08:09

Jamescun