PHP script - detect whether running under linux or Windows?

siliconpi picture siliconpi · May 4, 2011 · Viewed 76.4k times · Source

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.

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

Update

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

Answer

Sander Marechal picture Sander Marechal · May 4, 2011

Check the value of the PHP_OS constantDocs.

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

See as well: Possible Values For: PHP_OS and php_unameDocs:

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