I have written a rating system function for products and it used to throw a "Division by zero error" so in order to escape that error I have decided to implement it this way, I would like to know if it is a good way to escape from that error
$ratingNumber = 10;
$count = 0;
$average= $ratingNumber / isset($count)
CodePudding user response:
It wont work as you intended.
isset() checks if a variable, well, is set. You set 0 to $count then isset will always return true in this case.
You could do this instead:
function division($a, $b) {
if($b === 0) {
throw new InvalidArgumentException('Argument $b should not be 0.');
}
return $a/$b;
}
CodePudding user response:
You should avoid the division if $count = 0
. You can try it in the following way.
$ratingNumber = 10;
$count = 0;
if($count > 0){
$average= $ratingNumber / $count;
}