0

我在下面写了这个评级函数,想知道它是否可以变得更小/更简单或更好的编码,它使用显示一个基于函数中百分比数字的图像,称为

function rating( $number ) {

if ($number >= .0 && $number <= .099999) { 
  $star = '<img src="images/0.gif" alt="' . $number . '" />';
}

if ($number >= .10 && $number <= .199999){
  $star = '<img src="images/10.gif" alt="' . $number . '" />';
}

if ($number >= .20 && $number <= .299999){
  $star = '<img src="images/20.gif" alt="' . $number . '" />';
}

if ($number >= .30 && $number <= .399999){
  $star = '<img src="images/30.gif" alt="' . $number . '" />';
}

if ($number >= .40 && $number <= .499999){
  $star = '<img src="images/40.gif" alt="' . $number . '" />';
}

if ($number >= .50 && $number <= .599999){
  $star = '<img src="images/50.gif" alt="' . $number . '" />';
}

if ($number >= .60 && $number <= .699999) { 
  $star = '<img src="images/60.gif" alt="' . $number . '" />';
}

if ($number >= .70 && $number <= .799999){
  $star = '<img src="images/70.gif" alt="' . $number . '" />';
}

if ($number >= .80 && $number <= .899999){
  $star = '<img src="images/80.gif" alt="' . $number . '" />';
}

if ($number >= .90 && $number <= .999999){
  $star = '<img src="images/90.gif" alt="' . $number . '" />';
}

if ($number > .999999){
  $star = '<img src="images/100.gif" alt="' . $number . '" />';
}

return $star;
}

继承人如何使用它:

rating(0.51087)
4

1 回答 1

6

当然,它只需要一点数学。

function rating($number) {
    return '<img src="images/' . floor($number * 10) * 10 . '.gif" alt="' . $number . '" />';
}
于 2012-07-23T01:51:52.810 回答