4.2 PHP s OS and Version Constants


4.2 PHP's OS and Version Constants

You want to find out information about PHP and the OS at runtime and make some determinations based on that information.

Technique

Use 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; } ?> 

Comments

The 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.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net