Controlling Error Reporting On Different Servers With PHP

30th May 2008 - 1 minute read time

Having a testing server is quite a common practice, but what happens when you want to go live and you don't want all of your error messages displayed?

A good way of doing this is to create a variable that you can then use to detect what server your code is running on and set your error displaying accordingly. The following section of code will set a variable to true if the code is not running on the server name www.hashbangcode.com. This variable is then used to set the error reporting level.

  1. <?php
  2. $turnOnErrors = $_SERVER['SERVER_NAME'] != 'www.hashbangcode.com';
  3.  
  4. if ( $turnOnErrors ) {
  5. error_reporting(E_ALL);
  6. ini_set('display_errors', 1);
  7. } else {
  8. error_reporting(0);
  9. ini_set('display_errors', 0);
  10. }
  11. ?>

 

Add new comment

The content of this field is kept private and will not be shown publicly.