On Linux $_SERVER["_"]
contains path to PHP interprete开发者_StackOverflow社区r executable (for example /usr/bin/php -r 'echo $_SERVER["_"];'
will print /usr/bin/php
). On Windows XP with PHP 5.3 $_SERVER["_"]
is NULL
.
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
While not perfect, you could try this:
$_SERVER['phprc'] . 'php.exe'
which would give you something like
C:\Program Files\PHP\php.exe
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'));
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....
I dumped $_SERVER
and got pretty much options for any taste :)
array(100) {
...
["PHPBIN"]=>
string(43) "d:\openserver\modules\php\PHP-7-x64\php.exe"
["PHPDIR"]=>
string(36) "d:\openserver\modules\php\PHP-7-x64\"
["PHPRC"]=>
string(35) "d:\openserver\modules\php\PHP-7-x64"
["PHP_BIN"]=>
string(43) "d:\openserver\modules\php\PHP-7-x64\php.exe"
["PHP_BINARY"]=>
string(43) "d:\openserver\modules\php\PHP-7-x64\php.exe"
["PHP_BINDIR"]=>
string(36) "d:\openserver\modules\php\PHP-7-x64\"
["PHP_DIR"]=>
string(36) "d:\openserver\modules\php\PHP-7-x64\"
...
}
精彩评论