Skip to content

Instantly share code, notes, and snippets.

@aertmann
Created April 11, 2012 10:20
Show Gist options
  • Save aertmann/2358452 to your computer and use it in GitHub Desktop.
Save aertmann/2358452 to your computer and use it in GitHub Desktop.
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
foreach ($paths as $path) {
$phpExecutable = $path . DIRECTORY_SEPARATOR . 'php' . (isset($_SERVER['WINDIR']) ? '.exe' : '');
if (file_exists($phpExecutable) && is_file($phpExecutable)) {
exec(escapeshellarg($phpExecutable) . ' -v', $phpVersion);
if (substr(array_shift($phpVersion), 0, strlen('PHP ' . PHP_VERSION)) === 'PHP ' . PHP_VERSION) {
echo $phpExecutable;
}
}
}
@bwaidelich
Copy link

@aertmann
Copy link
Author

foreach ($paths as $path) {
$phpExecutable = $path . DIRECTORY_SEPARATOR . 'php' . (isset($_SERVER['WINDIR']) ? '.exe' : '');
if (file_exists($phpExecutable) && is_file($phpExecutable)) {
$phpVersion = trim(exec(escapeshellcmd($phpExecutable) . ' -r "echo PHP_VERSION;"'));
if ($phpVersion === PHP_VERSION) {
return $phpExecutable;
}
}
}

@kdambekalns
Copy link

The last version as nicely formatted code block:

<?php
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
foreach ($paths as $path) {
    $phpExecutable = $path . DIRECTORY_SEPARATOR . 'php' . (isset($_SERVER['WINDIR']) ? '.exe' : '');
    if (file_exists($phpExecutable) && is_file($phpExecutable)) {
        $phpVersion = trim(exec(escapeshellcmd($phpExecutable) . ' -r "echo PHP_VERSION;"'));
        if ($phpVersion === PHP_VERSION) {
            return $phpExecutable;
        }
    }
}

@bwaidelich
Copy link

BTW: This code is rather a reminder, "php -v" returns completely different strings that needs some parsing. but it would be great if we could get that to work sometime..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment