Monthly Archives: April 2010

Supressing Error Messages in PHP

Every once in awhile PHP decides to throw some errors.  Usually it’s because you didn’t do something right, but what if it’s throwing errors anyways?  What if you are on a production server and you need to suppress the errors?

In that case add the following the the top of your file, or include it as a header.

{code type=PHP}
ini_set(“display_errors”, 0);
ini_set(“log_errors”, 1);
{/code}

Optionally, if only a particular statement is throwing an error, you can do this:

{code type=PHP}

$myName = @get_name();

{/code}

The “@” symbol will suppress any errors that the calling function makes.

Remember, suppressing errors is no substitute for good coding.  But, sometimes you do what you have to do.

Showing All Errors in PHP

When I was first starting to create web sites with PHP, I struggled with getting error reporting turned on.  Turns out, that you can have it enable at the server, or in the document, or in both.  For development, it’s probably a good idea to have this at the top of script:

error_reporting(E_ALL);
ini_set('display_errors', '1');

This will allow php to display all errors, notices, and warnings.  Simple things like developing with error reporting full on like this help prevent security breaches and unknown application behavior.