Help needed with error handler for Division by Zero
Posted: Sun Aug 13, 2006 4:52 am
I need an error handler which will handle most errors normally based on the error_reporting() level but I want it to ignore the error that is caused due to division by zero, and let the script continue without echoing any message.
More specifically, I need it to work just as the "continue" statement would work because I will be using this for equations in a FOR loop, but that may not be possible directly. For example, if I have:
I need it to output:
I know I can probably use:
But since my script is much more complex than the above example I don't know what exactly the denominator is going to be as its not just a single variable. Therefore I cannot put such a statement there. Any idea how I can stop this?
Edit: I found this snippet online not sure if its useful:
More specifically, I need it to work just as the "continue" statement would work because I will be using this for equations in a FOR loop, but that may not be possible directly. For example, if I have:
Code: Select all
<?php
for($i = -3; $i <= 3; $i++){
echo 1/$i . "\n";
}
?>Code: Select all
-0.333333333333
-0.5
-1
1
0.5
0.333333333333Code: Select all
if($i == 0){ continue; }Edit: I found this snippet online not sure if its useful:
Code: Select all
<?php
function error_handler($error_no, $error_str, $error_file, $error_line) {
// not for @ errors
if (error_reporting() == 0) return;
// sort out what kind of error we have
switch($error_no) {
case E_NOTICE:
return;
break;
case E_USER_NOTICE:
$continue = TRUE;
$type = "Notice";
break;
case E_USER_WARNING:
case E_WARNING:
$continue = TRUE;
$type = "Warning";
break;
case E_USER_ERROR:
case E_ERROR:
$type = "Fatal Error";
break;
default:
$type = "Unknown Error";
break;
}
// put in error log
//error_log("[".date("d-M-Y H:i:s")."] PHP $type: $error_parts[0] error in line $error_line of file $error_file", 0);
if ($error_str == "Division by zero") {
//do nothing
}
else { // display
echo "\n<div>".nl2br($error_str)."</div>\n";
}
// halt for fatal errors
if (!isset($continue)) exit();
}
set_error_handler("error_handler");
?>