Add Ordinal Suffix PHP Function

<?php
 
  function addOrdinalNumberSuffix($num) {
    if (!in_array(($num % 100),array(11,12,13))){
      switch ($num % 10) {
        // Handle 1st, 2nd, 3rd
        case 1:  return $num.'st';
        case 2:  return $num.'nd';
        case 3:  return $num.'rd';
      }
    }
    return $num.'th';
  }
 
?>

Usage:

for ($i = 1; $i <= 10000; $i++){
  echo addOrdinalNumberSuffix($i) . "\t";
  if ($i % 10 == 0) {
    echo "\n";
  }
}
Advertisements