我正在使用一种方法来计算一些分数。这是以下方法:
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)