Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Too much code. Too many "if" statements that have to be executed every time.

Try:

  <?php
  function getRatingImage($rating)
  {
  $vals = ['zero','half','one','one_half','two','two_half','three','three_half','four','four_half','five'];
  $ndx = intval($rating * 2);
  if($ndx > 10){$ndx = 10};
  return "./" + $vals[$ndx] + ".jpg";
  }
  ?>


I totally agree that the use of all those ifs is bad. That said a switch statement will be faster than the code you posted(Though you were thinking in another language when your wrote your snippit methinks) if speed is your concern.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: