Current Location: Home> Latest Articles> How to use php_uname() to get current operating system information

How to use php_uname() to get current operating system information

M66 2025-06-02

PHP is a widely used server-side scripting language that not only handles dynamic web page content, but also interacts with the server through a variety of built-in functions. The php_uname() function is a very useful function provided by PHP to obtain detailed information about the current server operating system. This article will explain in detail how to use this function and demonstrate its application with an example.

1. Introduction to php_uname() function

The php_uname() function is used to obtain the name, version and other related information of the current operating system. This function will return different information on different operating systems such as Linux, Windows, and macOS. It has an optional parameter that specifies the degree of detail returned, defaulting to all available information.

2. Usage of php_uname() function

The basic syntax of the php_uname() function is as follows:

 string php_uname(string $mode = "a")
  • The $mode parameter is used to specify the returned information type. The modes that can be selected include:

    • "a" : Returns all operating system information (default value).

    • "s" : Returns the name of the operating system.

    • "r" : Returns the version of the operating system.

    • "v" : Returns the version number of the operating system.

    • "m" : Returns the machine's hardware architecture (for example: x86_64).

3. Sample code

Assuming we want to get the details of the server operating system, we can use the following code:

 <?php
// Get complete information about the operating system
echo php_uname();
?>

This code will output the complete information of the current server operating system. For example, in Linux systems, the output might be:

 Linux your-server-name 5.4.0-80-generic #90~18.04.1-Ubuntu SMP Thu Apr 8 08:45:57 UTC 2021 x86_64

If we just want to get the name of the operating system, we can use php_uname("s") :

 <?php
// Get the name of the operating system
echo php_uname("s");
?>

This will return an operating system name similar to Linux or Windows .

4. Application scenarios

Getting the server's operating system information through the php_uname() function can help developers perform some specific operations. For example:

  • System compatibility check : Determine whether certain functions are available or require different processing based on the version of different operating systems.

  • Debugging and Diagnosis : Understanding the operating system information can help determine the root cause of the problem when dealing with server failures or optimizations.

  • Security Enhancement : By understanding the detailed version of the operating system, developers can check for known vulnerabilities and take appropriate security measures.

5. Get more information

If you need to learn more about the operating system, in addition to php_uname() , you can also obtain other PHP functions, such as the phpinfo() function, which can display comprehensive information about the current PHP environment. You can choose the right tool as needed to get more system information.

6. Conclusion

php_uname() is a very useful PHP function that can help you quickly get detailed information about the server operating system. During the development process, by using this function reasonably, it can help you perform system detection, debugging and optimization.