Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php-cli: What is the best way to detect the hosting OS?

Tags:

php

I have a script which I want to run on windows under Cygwin and on Linux. I have to make distinction between the two running environment for some purposes. What is the best way to do it?

like image 472
Notinlist Avatar asked Feb 02 '10 14:02

Notinlist


People also ask

How does PHP detect operating system?

php_uname() returns a description of the operating system PHP is running on. This is the same string you see at the very top of the phpinfo() output. 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.

What is operating system in PHP?

As of PHP 7.2. 0 we have a new Predefined Constant to get the operating system family i.e. PHP_OS_FAMILY. It returns a string Either of 'Windows', 'BSD', 'OSX', 'Solaris', 'Linux' or 'Unknown'.


1 Answers

There is a pre-defined constant PHP_OS which will help, but only displays the OS that PHP was built on, not the OS it is running on.

php_uname is what you want to discover information about the current server running your code:

php_uname() returns a description of the operating system PHP is running on.

Specifically,

php_uname('s'); // Operating system name. eg. FreeBSD. 
like image 68
meagar Avatar answered Oct 24 '22 07:10

meagar