Monday, November 10, 2014

PHP 1st, 2nd, 3rd, 4th, 5th, 6th and so on PHP Add Ordinal Number Suffix

<?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';
}

for ($i = 1; $i <= 100; $i++){
echo addOrdinalNumberSuffix($i) . "\t";
if ($i % 10 == 0) {
echo "<br/>";
}
}

?>


Output :)