投票中零错误划分

时间:2014-12-22 17:25:39

标签: php wordpress

我得到零除错误这个功能。我该如何解决?

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

1 个答案:

答案 0 :(得分:1)

这应该适合你:

(如果$total_voting为零,则进行测试,在这种情况下返回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);
}