I've been asked a number of times in the past on how to best handle errors in php. There are two that I currently use. One would be to manually check the values of a variable to see if it conforms to various criteria and then echo to the page on the errors that were encountered. Another would be to trigger an error event which could be saved to the error log files, inserted into a database, saved to a custom error log or echoed out to the page. Here's some code which gives examples of both:
code1:
$errors = true;
if($myimage == 'var'){ $errors = false; } else{ echo "there was an error!";}
?>
code2:
// Override PHP.ini's setting of error reporting and intercept all errors with our own error handler
set_error_handler("myErrorHandler");
if (!function_exists("myErrorHandler")) {
function myErrorHandler($errno, $errstr, $errfile, $errline){
global $printErrors, $DBLogErrors, $emailErrors;
switch ($errno) {
case E_USER_ERROR : // 256
$errorMessage = "My ERROR [$errno] $errstr
\n";
$errorMessage .= " Fatal error on line $errline in file $errfile";
$errorMessage .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")
\n";
$errorMessage .= "Aborting...
\n";
echo $errorMessage;
//exit(1);
}
break;
default :
$errorMessage = "UNSPECIFIED ERROR [$errno] $errstr
\n";
$errorMessage .= " error on line $errline in file $errfile";
$errorMessage .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")
\n";
$errorMessage .= "Aborting...
\n";
echo $errorMessage;
//exit(1);
break;
}
/* Don't execute PHP internal error handler */
return true;
}
}
?>
No comments:
Post a Comment