Skip to content

Instantly share code, notes, and snippets.

@cjsewell
Created November 9, 2014 08:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjsewell/39b60065ae2af677ceb1 to your computer and use it in GitHub Desktop.
Save cjsewell/39b60065ae2af677ceb1 to your computer and use it in GitHub Desktop.
PHP function to try and find the php executable on the current system. Kind of redundant now 5.4 has PHP_BINARY, but this is good for backwards compatibility
function findPHP() {
if (defined('PHP_BINARY') && is_executable(PHP_BINARY)) {
$res = PHP_BINARY;
} else {
$which = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'where' : 'which';
$outputArr = [];
$whichReturn = false;
$res = exec($which . " php 2>&1", $outputArr, $whichReturn);
if ($whichReturn !== 0) {
$res = false;
$lookIn = array(
defined('PHP_BINDIR') ? PHP_BINDIR : '',
'c:\xampp',
'd:\xampp',
getenv('ProgramFiles'),
getenv('ProgramFiles(x86)'),
getenv('ProgramW6432')
);
$suffixes = array(
'php',
'/php/php',
'/php/bin/php',
);
$extensions = array(
"",
".exe"
);
foreach ($lookIn as $folder) {
foreach ($suffixes as $suffix) {
foreach ($extensions as $extension) {
$php = $folder . $suffix . $extension;
if (is_executable($php)) {
$res = realpath($php);
break 3;
}
}
}
}
}
}
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment