1

I get division by zero error this functions. How do I fix it?

function percent($liked,$total_voting){
$liked = (int)$liked;
$total_voting = (int)$total_voting;
return ceil(($liked/$total_voting)*100);
Brad
  • 39
  • 6

1 Answers1

1

This should work for you:

(It's testing if $total_voting is zero and returns in this case false)

function percent($liked, $total_voting) {

    if($total_voting == 0)
        return false;

    $liked = (int)$liked;
    $total_voting = (int)$total_voting;

    return ceil(($liked/$total_voting)*100);
}
Rizier123
  • 58,877
  • 16
  • 101
  • 156