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

2022-01-11 00:00:00 operating-system windows linux detect php

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

I have a PHP script that may be placed on a windows system or a linux system. I need to run different commands in either case.

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

How can I detect which environment I am in? (preferably something PHP rather than clever system hacks)

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

To clarify, the script is running from the command line.

推荐答案

检查 PHP_OS 常量文档.

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

It will give you various values on Windows like WIN32, WINNT or Windows.

另请参阅:可能的值:PHP_OS 和 php_uname文档:

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}

相关文章