Quote:
Originally Posted by Cjwinnit
My thoughts:
PHP Code:
function ordinal ( $ordnum ) { $ordinalsuffixes = array("th","st","nd","rd"); for($i=1;$i<=3;$i++) { if(($ordnum % 10 == $i) && ($ordnum % 100 != 10+$i )) return $ordinalsuffixes[$i]; } return $ordinalsuffixes[0] ; }
If you wanted an error-check you could insert this as the first line in the func.:
PHP Code:
if(!((gettype($ordnum) == "integer") && ($ordnum > 0))) return false;
I think I win for the shortest code. Cookie? 
|
You definitely win for least-readable!
Side note: I would recommend never using
gettype() for type checking. The PHP manual states that the returned string values may change in future releases.
I would use
is_int( $ordnum ) rather than
'integer' == gettype( $ordnum )
Something else I found unusual was your use of negation:
! ( gettype( $ordnum ) == "integer" ) as opposed to
gettype( $ordnum ) != "integer"