我有一个 PHP 脚本,可以放在 windows 系统或 linux 系统上。无论哪种情况,我都需要运行不同的命令。
如何检测我所处的环境? (最好是 PHP 而不是聪明的系统黑客)
更新
为了澄清,脚本是从命令行运行的。
最佳答案
检查 PHP_OS
constantDocs 的值。
它将在 Windows 上为您提供各种值,例如 WIN32
、WINNT
或 Windows
。
另见:Possible Values For: PHP_OS 和 php_uname
Docs:
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo 'This is a server using Windows!';
} else {
echo 'This is a server not using Windows!';
}
关于PHP 脚本 - 检测是否在 linux 或 Windows 下运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5879043/