获取操作系统类型
-
php_uname
返回运行 PHP 的系统的有关信息。
该方法接受一个字符参数,默认为
'a'
,所有参数:- 'a':此为默认。包含序列 "s n r v m" 里的所有模式。
- 's':操作系统名称。例如: FreeBSD。
- 'n':主机名。例如: localhost.example.com。
- 'r':版本名称,例如: 5.1.2-RELEASE。
- 'v':版本信息。操作系统之间有很大的不同。
- 'm':机器类型。例如:i386。
获取操作系统名称:
php_uname('s')
注意,在Windows下返回值可能有以下几种:
- WINNT
- WIN32
- Windows
- Windows NT
if (strtoupper(substr(php_uname('s'), 0, 3)) === 'WIN') { echo 'This is a server using Windows!'; } else { echo 'This is a server not using Windows!'; }
-
PHP_OS
PHP_OS是PHP的预定义常量,返回当前操作系统名称。
返回值与
php_uname('s')
相同。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_FAMILY
从PHP 7.2.0开始提供。
将操作系统分组,同类型操作系统具有唯一的返回值,返回值包含以下内容:
'Windows', 'BSD', 'Darwin', 'Solaris', 'Linux', 'Unknown'
if (PHP_OS_FAMILY === 'Windows') { echo 'This is a server using Windows!'; } else { echo 'This is a server not using Windows!'; }
-
PATH_SEPARATOR
在Windows上返回
':'
,其它系统返回';'
if (PATH_SEPARATOR !== ':') { echo 'This is a server using Windows!'; } else { echo 'This is a server not using Windows!'; }
此常量一般用来判断操作系统是否为Windows。
/** * @return bool */ function isWinOs() { return PATH_SEPARATOR !== ':'; }
-
DIRECTORY_SEPARATOR
在Windows上返回
'\'
,其它系统返回'/'
用法同PATH_SEPARATOR。
本作品采用《CC 协议》,转载必须注明作者和本文链接