How to get the OS on which PHP is running?

How to get the OS on which PHP is running?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Detecting the Underlying Operating System in PHP

Determining the operating system within a PHP script is a common requirement when developing cross-platform utilities or scripts that need to behave differently on Unix-like systems versus Windows. While phpinfo() provides a wealth of data, parsing its output to extract OS details is inefficient and brittle. The built-in PHP_OS constant offers the most direct solution, but understanding its behavior and limitations is crucial for writing robust code.

The PHP_OS constant returns a string representing the operating system name based on the platform where PHP was compiled. On a Linux or macOS server, this typically returns something like Linux or Darwin. On Windows, it usually returns WindowsNT. You can access this value directly in your script:

$os = PHP_OS;
echo $os;

This approach is reliable because the constant is set at compile time. It does not dynamically detect the running environment after deployment; rather, it reflects how the PHP binary was built. If you install PHP from a package manager like apt on Ubuntu, PHP_OS will return Linux. If you download a pre-compiled binary from php.net for Windows, it will return WindowsNT.

However, relying solely on PHP_OS can be misleading if you are using a containerized environment or a specific build configuration. For instance, Docker containers often run Linux kernels regardless of the host OS. In such cases, PHP_OS correctly identifies the kernel running inside the container. If your application logic depends on specific Unix commands (like ls or chmod) versus Windows equivalents (dir or icacls), checking PHP_OS is the standard first step to branching logic accordingly.

For more granular details, you might consider inspecting the environment variables available to the PHP process. The UNAME_S constant provides a similar value to PHP_OS, often containing the full distribution name (e.g., Linux x86_64). This can be useful if you need to distinguish between specific distributions like Alpine Linux and CentOS, although this level of detail is rarely necessary for general scripting.

$osName = PHP_OS;
$unameS = UNAME_S ?? ''; // Fallback if not defined in older versions

if ($osName === 'Linux') {
    // Execute Unix-specific logic
} elseif ($osName === 'WindowsNT') {
    // Execute Windows-specific logic
}

One significant pitfall to avoid is assuming that PHP_OS alone guarantees compatibility with specific file path separators or command syntax. While the constant tells you the OS, it doesn't inform you about the specific shell environment or whether PHP is running as a CGI script, FastCGI process, or CLI executable. These execution contexts can affect how environment variables like $PATH are populated, which indirectly impacts your ability to run system commands.

If you require deeper insight into the kernel version or architecture, UNAME_S combined with UNAME_R and UNAME_M provides a more complete picture than PHP_OS alone. This is particularly relevant when debugging issues where PHP reports one OS but fails to execute a command that works in the native shell.

In modern development, especially when using frameworks like Laravel, platform detection is often abstracted away into service containers or specific helper classes. However, for standalone scripts or legacy codebases, PHP_OS remains the canonical, lightweight method to identify the runtime environment without invoking external processes or loading heavy libraries. Always verify that your branching logic handles edge cases, such as 32-bit versus 64-bit builds, though PHP_OS itself does not distinguish architecture; you would need to inspect UNAME_M for that specific detail.

Our network