I want to run a PHP cli program from within PHP cli. On some machines where this will run, both php4 and php5 are installed. If I run the outer program as
You could use phpversion()
to get the current version of PHP before you execute the "inner" script.
http://php.net/manual/en/function.phpversion.php
Unfortunately PHP_BINARY is returning the httpd binary (on windows XAMPP), so I'm back to using paths...
if (defined('PHP_BINARY') && PHP_BINARY && in_array(PHP_SAPI, array('cli', 'cli-server')) && is_file(PHP_BINARY)) {
return PHP_BINARY;
} else if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
foreach ($paths as $path) {
if (substr($path, strlen($path)-1)==DIRECTORY_SEPARATOR) {
$path=substr($path, 0, strlen($path)-1);
}
if ( substr($path, strlen($path) - strlen('php')) == 'php' ) {
$response=$path.DIRECTORY_SEPARATOR.'php.exe';
if (is_file($response)) {
return $response;
}
} else if ( substr($path, strlen($path) - strlen('php.exe')) == 'php.exe' ) {
if (is_file($response)) {
return $response;
}
}
}
} else {
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
foreach ($paths as $path) {
if (substr($path, strlen($path)-1)==DIRECTORY_SEPARATOR) {
$path=substr($path, strlen($path)-1);
}
if ( substr($path, strlen($path) - strlen('php')) == 'php' ) {
if (is_file($path)) {
return $path;
}
$response=$path.DIRECTORY_SEPARATOR.'php';
if (is_file($response)) {
return $response;
}
}
}
}
return null;
This answer was helpful for tracking down a php location: How to get the path of the PHP BIN from PHP?
It's also windows and linux friendly :)
I know this question is a bit old, but it came up while looking for a shorter way of doing this.
Unfortunately I can't find a shorter way, but this works pretty well and is multi OS/PHP version compatible.
$lookIn could probably be extended to include more common locations.
https://gist.github.com/cjsewell/39b60065ae2af677ceb1
Hope it helps someone
I think the bestconstant PHP_BINARY. With PHP 5.5.12
On my server I've php 5.3.14.
I've found a predefined constant: PHP_BIN_DIR
Then, supposing the file name of the executable file is always 'php', $php_cmd = PHP_BIN_DIR.'/php'
point to my PHP executable file.