Created
November 9, 2014 08:05
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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