PHP script - detect whether running under linux or Windows?

84,200

Solution 1

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!';
}

Solution 2

You can check if the directory separator is / (for unix/linux/mac) or \ on windows. The constant name is DIRECTORY_SEPARATOR.

if (DIRECTORY_SEPARATOR === '/') {
    // unix, linux, mac
}

if (DIRECTORY_SEPARATOR === '\\') {
    // windows
}

Solution 3

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

seems like a bit more elegant than the accepted answer. The aforementioned detection with DIRECTORY_SEPARATOR is the fastest, though.

Solution 4

Starting with PHP 7.2.0 you can detect the running O.S. using the constant PHP_OS_FAMILY:

if (PHP_OS_FAMILY === "Windows") {
  echo "Running on Windows";
} elseif (PHP_OS_FAMILY === "Linux") {
  echo "Running on Linux";
}

See the official PHP documentation for its possible values.

Solution 5

Note that PHP_OS reports the OS that PHP was built on, which is not necessarily the same OS that it is currently running on.

If you are on PHP >= 5.3 and just need to know whether you're running on Windows or not-Windows then testing whether one of the Windows-specific constants is defined may be a good bet, e.g.:

$windows = defined('PHP_WINDOWS_VERSION_MAJOR');
Share:
84,200

Related videos on Youtube

siliconpi
Author by

siliconpi

Updated on November 11, 2020

Comments

  • siliconpi
    siliconpi over 3 years

    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.

    • caw
      caw over 4 years
      After performing benchmarks, it seems that any differences in performance between \strncasecmp(\PHP_OS, 'WIN', 3) === 0, \strtoupper(\substr(\PHP_OS, 0, 3)) === 'WIN' and \stripos(\PHP_OS, 'WIN') are below 15%. Since all three solutions only take roughly 100 nanoseconds, this would be a micro-optimization, anyway. So choose whatever solution you like. Finally, you may argue that \strncasecmp(\PHP_OS, 'WIN', 3) === 0 is the most readable.
  • Shabbyrobe
    Shabbyrobe almost 13 years
    What value would it be if it's Windows? There is nothing in the docs that I can see.
  • Sander Marechal
    Sander Marechal almost 13 years
  • OMA
    OMA over 11 years
    Sorry, but this doesn't work right if you're using a Mac server, since in Mac you get a string that contains "DARWIN", which also contains "WIN", so in a Mac server you'll get "This is a server using Windows!" which is not true.
  • Ondřej Bouda
    Ondřej Bouda over 11 years
    Well, this is not quite true. Notice that strncasecmp takes the first n characters, not just any n characters. If you tried it, you would have found out that strncasecmp('DARWIN', 'WIN', 3) == 0 evaluates to false.
  • Achim
    Achim about 10 years
    Unfortunately this does not work with Windows 7 and Zend Server. In this case DIRECTORY_SEPARATOR is also '\'
  • mpen
    mpen over 9 years
    @Achim What do you mean? Zend Server is server software, not an OS. Windows 7 should return \.
  • StanE
    StanE over 9 years
    1. Correct is not where PHP was built ON but for what it was built FOR 2. This applies to the defined windows related constant the same way...
  • jpaugh
    jpaugh over 7 years
    Although the code is simple enough, this answer does not stand on its own: A great answer doesn't force one to read the question when coming from a search engine, because it is self-contained.
  • Stalinko
    Stalinko over 7 years
    Anyway PHP built for Windows can not be run on unix, can it?
  • Titus
    Titus over 7 years
    strpos(__FILE__,92) exploits the same behaviour; PATH_SEPARATOR>":" returns true for Windows too (PATH_SEPARATOR is ":" on all other OSs).
  • Mario Lurig
    Mario Lurig over 6 years
    Turned this into a function: function is_linux(){return (DIRECTORY_SEPARATOR == '/') ? true : false;}
  • John Hunt
    John Hunt over 5 years
    stripos(PHP_OS, 'WIN') === 0 can be used instead, arguably more readable / simple.
  • John Hunt
    John Hunt over 5 years
    @dw1 - it would still work as it searches for "win" at the beginning.
  • Lucas Bustamante
    Lucas Bustamante almost 3 years
    @Stef According to 3v4l, it's only available in PHP 7.2 3v4l.org/CBpG4
  • Redzarf
    Redzarf over 2 years
    And with php8 we now have if (str_starts_with(strtoupper(PHP_OS), 'WIN')) {