How to test if your server is running Windows from PHP

If we’re executing shell commands via PHP we need to know if the server is running Windows or Linux. Thanks to a magic constant in PHP we can find out like this:

This will give us a single value like

  • Linux
  • Darwin
  • Windows
  • WINNT

With that info at hand, we can write a function that delivers a boolean result (courtesy of the PHP manual):

This takes the output of PHP_OS and looks at the first three characters turned into upper case. If those are identical to WIN it’s Windows – and if not we assume it’s a Linux flavour.

If you need a more detailed information about your environment consider using php_uname():

This will give you the above, plus server name, kernel version, release and local server time.

About Jay Versluis

Jay is a medical miracle known as a Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

Add your voice!