Manso Trick: Detect the Operating System in PHP

There could be a lot of reasons to detect the OS in PHP, and I’m pretty aware that these reasons could open a discussion by themselves. Anyway, it could be useful to do some tweaks or optimizations, to run one commands or anothers, or simply  it can be useful as additional information for webmasters, developers or operations department.

Anyway, we are all grown ups, so you know what you are doing. Now, imagine you need to know this. There are several ways (the more creative you are, the more ways you will find). The first way, and most simple and complete, is by using the php_uname function. The php_uname is similar to the unix command uname, and with this function we can know the  operating system, the version of the OS, the release number, the host name, and the hardware architecture  (i386, amd64, …). It has all that you need 😉

On the other hand, if you don’t want to use this pretty command, you can directly use the PHP_OS constant, which only includes the operating system name, but in most cases will be enough (it is equivalent to call php_uname(‘s’)).

Now you can try these two examples:

echo php_uname() . "\n";
echo PHP_OS . "\n";

The funny thing is that probably you would never need to know the OS, so you can skip the whole post 😛

Trackback URL

, , ,

Comments are closed.