note 42083 deleted from function.is-numeric by crell
| From: | crell@php.net | Date: | Mon, 18 Apr 2022 14:51:49 +0000 |
| Subject: | note 42083 deleted from function.is-numeric by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218263@lists.php.net to get a copy of this message | ||
Note Submitter: renimar no spam at nospam yahoo dot com
----
A little function to ordinalize numbers using is_numeric() and accounting for the numbers in the
teens.
<?php
function ordinalize($num) {
if (!is_numeric($num))
return $num;
if ($num >= 11 and $num <= 19)
return $num."th";
elseif ( $num % 10 == 1 )
return $num."st";
elseif ( $num % 10 == 2 )
return $num."nd";
elseif ( $num % 10 == 3 )
return $num."rd";
else
return $num."th";
}
// Demo
for ($i=1; $i<=25; $i++) {
print ordinalize($i) . " ";
}
// The loop returns:
// 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th
// 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd
// 23rd 24th 25th
?>