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


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.