如何获取运行 PHP 的操作系统?

2022-01-11 00:00:00 operating-system php

为了构建一个 unix/dos 特定的脚本,我需要知道我在哪种操作系统上.

For building a unix/dos specific script I need to know on which kind of operating system I am.

我如何获得这些信息?
phpinfo(); 告诉我更多,但不是很清楚我是否在 unix 上运行.

How do i get this information?
phpinfo(); tells me a lot more and not very clear whether I'm running on unix or not.

推荐答案

PHP有很多预定义常量,通常很有用.

PHP has many predefined constants that are often useful.

这里,PHP_OS 就是你要找的那个.

Here, PHP_OS is the one you are looking for.


例如,在我当前的机器上,这段代码:


For instance, on my current machine, this code :

var_dump(PHP_OS);

给:

string 'Linux' (length=5)


你有一些例子和比较 php_uname 函数可以让你在 php_uname 的手册页;例如(引用):

<?php
echo php_uname();
echo PHP_OS;

/* Some possible outputs:
Linux localhost 2.4.21-0.13mdk #1 Fri Mar 14 15:08:06 EST 2003 i686
Linux

FreeBSD localhost 3.2-RELEASE #15: Mon Dec 17 08:46:02 GMT 2001
FreeBSD

Windows NT XN1 5.1 build 2600
WINNT
*/

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_OS不变,但请记住这一点常量将包含操作系统 PHP 构建.

For the name of just the operating system, consider using the PHP_OS constant, but keep in mind this constant will contain the operating system PHP was built on.

相关文章