php_uname() is a built-in function in PHP that obtains relevant information about the operating system. It can return different degrees of operating system information, depending on the passed parameters. This function is very useful for developers to debug and check the running environment, especially when developing in a multi-platform environment.
string php_uname(string $mode = "a")
$mode (optional): This is a character parameter that specifies the degree of detail of returning operating system information. It can take the following values:
'a' : Returns all operating system information (default value).
's' : Returns the operating system name.
'r' : Return to the release version of the operating system.
'v' : Returns the operating system version information.
'm' : Returns the hardware type of the machine.
The php_uname() function will return a string representing system information. The specific content will vary according to the passed $mode parameter.
Here is a simple example showing how to use php_uname() to get different operating system information:
<?php
// Get all operating system information
echo php_uname(); // Sample output:Linux localhost 4.15.0-54-generic #58-Ubuntu SMP Fri Oct 5 14:44:42 UTC 2018 x86_64
// Get the operating system name
echo php_uname('s'); // Sample output:Linux
// Get the operating system version
echo php_uname('r'); // Sample output:4.15.0-54-generic
// Get the operating system version information
echo php_uname('v'); // Sample output:#58-Ubuntu SMP Fri Oct 5 14:44:42 UTC 2018
// Get the machine hardware type
echo php_uname('m'); // Sample output:x86_64
?>
In multi-platform development, it is very important to judge the running platform. You can use php_uname() to determine whether it runs on Linux, Windows, or other operating systems:
<?php
$os = php_uname('s');
if (strpos($os, 'Linux') !== false) {
echo "Running in Linux On the system";
} elseif (strpos($os, 'Windows') !== false) {
echo "Running in Windows On the system";
} else {
echo "Running in其他On the system";
}
?>
You can get the kernel version information of the operating system by passing the 'r' parameter, which is very helpful when debugging certain system features or optimizing code:
<?php
echo "Operating system kernel version:".php_uname('r');
?>
Combining php_uname() and other PHP functions, you can obtain complete server environment information for diagnosis and debugging. For example, you can combine phpversion() to get the PHP version, and combine getenv() to get environment variables, and finally combine them into a complete system information output:
<?php
echo "operating system:".php_uname('s')."\n";
echo "operating systemVersion:".php_uname('r')."\n";
echo "PHP Version:".phpversion()."\n";
echo "Web Server environment:".getenv('SERVER_SOFTWARE')."\n";
?>
For some cross-platform development tasks, php_uname() can help you confirm whether the current PHP code is compatible with the system environment. For example, if you are developing a Linux-specific tool, you can make sure that the program only runs on the appropriate platform by checking the operating system type:
<?php
if (php_uname('s') !== 'Linux') {
die("This program only applies to Linux system");
}
?>
php_uname() is a very useful function in PHP. It can provide detailed information about the operating system and help developers understand the running environment. It can provide valuable help whether it is during the debugging stage or in cross-platform development.
By mastering the basic usage of php_uname() and some practical tips, you can develop, debug and deploy PHP programs more efficiently. If you have more needs for PHP system information acquisition, you might as well explore other uses of this function, and even combine it with other system functions to obtain richer environmental data.