I'm thinking about writing a simple function to handle all my division for my web application. The main reason is to catch division by 0 errors. I'm thinking I'll just return 0 when division by 0 is attempted.
Am I completely thinking wrong here?
The web application is very extensive and currently used by 5 different businesses. It records and processes over 10,000 different records every day. So saying stop dividing by zero is simply not a solution.
Try something like this:
function division($dividend, $divisor) {
if ($divisor == 0) {
//change this return value to whatever fits your code
return 0;
} else {
return $dividend / $divisor;
}
}
Usage:
echo division(4, 2); // 2
echo division(9, (5-1)); // 2.25
echo division(1, 0); // 0
You could also throw an Exception if the divisor equals zero or return false
, for example, and treat the error in whatever way you need.