Posted By

feeela on 08/24/10


Tagged

rank


Versions (?)

alternative notation of "Make Position Number", using less if-statements


 / Published in: PHP
 

URL: http://snipplr.com/view/39427/make-position-number/

  1. function numerals($number)
  2. {
  3. if(strlen($number) > 1 && 1 == substr($number, -2, -1))
  4. {
  5. $lastDigit = 0;
  6. }
  7. else
  8. {
  9. $lastDigit = substr($number, -1);
  10. }
  11.  
  12. switch($lastDigit)
  13. {
  14. case 3:
  15. $numeralAppendix = 'rd';
  16. break;
  17. case 2:
  18. $numeralAppendix = 'nd';
  19. break;
  20. case 1:
  21. $numeralAppendix = 'st';
  22. break;
  23. default:
  24. $numeralAppendix = 'th';
  25. break;
  26. }
  27. return $number . $numeralAppendix;
  28. }

Report this snippet  

You need to login to post a comment.