Controlling Error Reporting On Different Servers With PHP

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.
CAPTCHA
2 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.