4.2 PHP's OS and Version Constants
TechniqueUse PHP's built-in constants PHP_OS and PHP_VERSION and make choices based on that information: <?php switch (PHP_OS) { case "WIN32": win32_function(); break; case "Linux": linux_function(); break; case "OS/2": os2_function(); break; } ?> CommentsThe following will help you if you need to write different types of functions for different operating systems. This is especially useful if you are making a one- size -fits-all script, and you don't know the OS on which it will be run. However, note that some configurations of PHP do not support this constant, so you might want to use the following error checking: if (!defined(PHP_OS)) { die("OS Detection cannot be done automatically"); } // now use your funky OS detection code ?> To detect the version of PHP being used, your code can be similar to the OS detection code, except you replace PHP_OS with PHP_VERSION and you will have different cases. I must mention that this is not the best way to get information about your system and PHP in general. If you want complete information about PHP and your OS, use the phpinfo() function like so: <?php phpinfo(); ?> Just open this page in your browser and it will print out all information available about PHP, including extensions, your configuration options, environment, PHP variables , Apache environment, and HTTP header information. |