3

我正在使用一种方法来计算一些分数。这是以下方法:

public function getIntScore($name, $int){
    switch ($name) {
        case "bedrooms":
            $maxscore = 15;
            break;
        case "living_size":
            $maxscore = 10;
            break;
        case "property_size":
            $maxscore = 10;
            break;
        case "date_of_construction":
            $maxscore = 3;
            break;
    }
    $houseattribute = (int) $this->$name;
    $difference = abs($houseattribute - $int);
    if ($difference == 0) {
        return $maxscore;
    }
    $score = ($difference / $houseattribute) * $maxscore;
    return round($score);
}

但是,这给了我一个“除以零”错误。我在计算之前检查了变量的值,它们都不是零。

var_dump($difference, $houseattribute, $maxscore) 

输出:

int(2) int(3) int(15) 
4

2 回答 2

1

确保测试空值:

$houseattribute = (int) $this->$name;
if (empty($houseattribute)) {
   throw new Exception('House attribute is zero.');
}
于 2012-10-16T11:34:32.643 回答
0

解决了。我傻到忘记了我正在遍历一个数组,第一个$houseattribute3. 我的数组中有一个项目 where $houseattributeis 0,但因为我exit;在转储变量后使用了我不知道它是。我将其更改为正整数,但它不起作用。

于 2012-10-16T13:20:34.257 回答