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.

<?php
 $turnOnErrors = $_SERVER['SERVER_NAME'] != 'www.hashbangcode.com';
 
 if ( $turnOnErrors ) {
  error_reporting(E_ALL);
  ini_set('display_errors', 1);
 } else {
  error_reporting(0);
  ini_set('display_errors', 0);
 }
?>

 

Add new comment

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