Sunday, October 17, 2010

PHP Error Reporting

Displaying errors in PHP is easy (and I find it's really easy creating errors, but that's another blog post).

I usually use the following code in my PHP file to display errors and figure out why my code isn't working.

// set php to display errors, this config is changed dynamically at run time by applying the code to a .php file
ini_set('display_errors',1);

// set php to display all errors AND strict rules which is not included in e_all
error_reporting(E_ALL|E_STRICT);

// show all except notices
error_reporting(E_ALL ^ E_NOTICE);

Normally I wrap these commands in an if statement so I can turn it on/off as required.

$devmode = true;

if($devmode===true){
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
}

Setting a variable for development or reporting mode means you can use if-statements in your code to display all sorts of useful information, and hide all of your testing/reporting output by changing one variable in code.

if($devmode===true){
print '<pre>';
print_r($some_array_i_want_to_see);
print '</pre>';
}

0 comments:

Post a Comment