gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:42:59 26 4
gpt4 key购买 nike

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

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

最佳答案

PHP 有很多 predefined constants这通常很有用。

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


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

var_dump(PHP_OS);

给:

string 'Linux' (length=5)


你有一些例子和比较 php_uname 函数可以让你在 manual page of 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!';
}

该页面还显示:

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.

关于php - 如何获取运行 PHP 的操作系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1482260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com