PHP 脚本 - 检测是否在 linux 或 Windows 下运行?

标签 php windows linux operating-system detect

我有一个 PHP 脚本,可以放在 windows 系统或 linux 系统上。无论哪种情况,我都需要运行不同的命令。

如何检测我所处的环境? (最好是 PHP 而不是聪明的系统黑客)

更新

为了澄清,脚本是从命令行运行的。

最佳答案

检查 PHP_OS constantDocs 的值。

它将在 Windows 上为您提供各种值,例如 WIN32WINNTWindows

另见:Possible Values For: PHP_OSphp_unameDocs:

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/

相关文章:

php - Silverstripe - cms 字段未保存到数据库

python-3.x - 哪个版本的 Miniconda 有适用于 64 位 Windows 的 Python 3.6?

linux - 拦截 DNS 服务器上的数据

php - 如果访问日期是<今天的日期,则更新 "status"单元格php mysql

php - 循环中的 Codeigniter 嵌套查询

.net - Powershell:管道输入到通过调用运算符执行的命令

linux - Bash 获取等待进程的退出代码

linux - 安装驱动程序会增加 linux 内核内存占用吗?

java - Android应用登录注册报错

windows - 等待托管代码中的消息