Page 1 of 1

Help needed with error handler for Division by Zero

Posted: Sun Aug 13, 2006 4:52 am
by Verminox
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:

Code: Select all

<?php
for($i = -3; $i <= 3; $i++){
        echo 1/$i . "\n";
}
?>
I need it to output:

Code: Select all

-0.333333333333
-0.5
-1
1
0.5
0.333333333333
I know I can probably use:

Code: Select all

if($i == 0){ continue; }
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:

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");


?>

Posted: Sun Aug 13, 2006 7:44 am
by volka
You might try

Code: Select all

<?php
$r = @bcdiv(1, 0);
var_dump($r);
?>
or

Code: Select all

<?php
$r = @(1/0);
var_dump($r);
?>
http://de2.php.net/bc

Posted: Sun Aug 13, 2006 8:07 am
by Verminox
Ahh well I did not want BC, because it is much more complex than $a/$b. I'm working on a graph script to plot any f(x) so I can't be sure what the variable user input can be.

All that the @ operator does is hide the error message, the script still continues to run. I need to break the loop.


Edit: Got it.

Code: Select all

$y = @f($x);
if($y === NULL){ continue; }
So @ did help :)

Thanks a lot mate.

I might post the entire script in Coding Critique later today if I finish ;)