Error-Handling in PHP
My goal for today: master error-handling in PHP. I've been patching together my own error-handling procedures in my scripts, but I get the feeling I'm reinventing the wheel for certain things and missing a lot of useful built-in functionality for others.
Good advice for debugging :
In your init.php script:
if (_DEBUG_) {
set_error_handler ('debug_error_handler');
}
else {
set_error_handler ('nice_error_handler');
}
_DEBUG_ would then be a constant equating to 1 or true, I take it.
Example of an error-handler (this one suppresses messages for unset array indicis):
function error_handler($errno, $errstr, $errfile, $errline, $errctx) {
if ($errno == E_NOTICE && substr($errstr, 0, 17) == "Undefined index: ") return;
echo "\nerror_handler:\n\terrno=$errno\n\terrstr=$errstr\n";
echo "\terrfile=$errfile\n\terrline=$errline\n";
if ($errno & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR)) die();
}
set_error_handler("error_handler");
error_reporting(E_ALL);
Also, note this:
jernberg at fairytale dot se
27-Feb-2003 03:27
tip: if you want your error_reporting()-setting to work with your own error handler you could simply check the error number against the current error bitmask.
function myErrorHandler( $errno, $errstr, $errfile, $errline )
{
$replevel = error_reporting();
if( ( $errno & $replevel ) != $errno )
{
// we shall remain quiet.
return;
}
echo( "error....." );
}
A nice tutorial: Devshed
Useful links
Good advice for debugging :
In your init.php script:
if (_DEBUG_) {
set_error_handler ('debug_error_handler');
}
else {
set_error_handler ('nice_error_handler');
}
_DEBUG_ would then be a constant equating to 1 or true, I take it.
Example of an error-handler (this one suppresses messages for unset array indicis):
function error_handler($errno, $errstr, $errfile, $errline, $errctx) {
if ($errno == E_NOTICE && substr($errstr, 0, 17) == "Undefined index: ") return;
echo "\nerror_handler:\n\terrno=$errno\n\terrstr=$errstr\n";
echo "\terrfile=$errfile\n\terrline=$errline\n";
if ($errno & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR)) die();
}
set_error_handler("error_handler");
error_reporting(E_ALL);
Also, note this:
jernberg at fairytale dot se
27-Feb-2003 03:27
tip: if you want your error_reporting()-setting to work with your own error handler you could simply check the error number against the current error bitmask.
function myErrorHandler( $errno, $errstr, $errfile, $errline )
{
$replevel = error_reporting();
if( ( $errno & $replevel ) != $errno )
{
// we shall remain quiet.
return;
}
echo( "error....." );
}
A nice tutorial: Devshed
Useful links
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home