note 51089 added to function.is-numeric
| From: | lukesneeringer at gmail dot com | Date: | Sat, 19 Mar 2005 04:32:32 +0000 |
| Subject: | note 51089 added to function.is-numeric | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86678@lists.php.net to get a copy of this message | ||
Regarding renimar at yahoo's function to yield ordinal numbers, the function lacks one thing.
It accounts for numbers in the teens only if the number is below 100. If you used this function and
gave 212 as the input, it would give 212nd, and not 212th. (Also, checking for numbers between 11
and 13 is sufficient, since 14-19 yield th either way.)
Therefore,
<?php if ($num >= 11 and $num <= 19) ?>
should be changed to...
<?php if ($num % 100>= 11 and $num % 100 <= 13) ?>
It will then work perfectly all the time.
Here's the entire function with the one line changed:
<?php
function ordinalize($num) {
if (!is_numeric($num))
return $num;
if ($num % 100 >= 11 and $num % 100 <= 13)
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 // $num % 10 == 0, 4-9
return $num."th";
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.is-numeric.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51089
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51089&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51089&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51089&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51089&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51089&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51089&report=yes
Search -- http://master.php.net/manage/user-notes.php