Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Since this has already been resurrected with a fairly relevant post, I'll add:

    I wrote a class for codingforums.com that might be of interest. It's archived here: http://www.codingforums.com/archive/.../t-180473.html
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  2. #17
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    I wanna play too...

    PHP Code:
    function num_st_nd($n){
        if(
    $n == || ($n 10 && ($n-11)%10 == && substr($n,-2) != 11)) return 'st';
        if(
    $n == || ($n 10 && ($n-12)%10 == && substr($n,-2) != 12)) return 'nd';
        if(
    $n == || ($n 10 && ($n-13)%10 == && substr($n,-2) != 13)) return 'rd';
        return 
    'th';


  3. #18
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by Cjwinnit View Post
    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"

    Last edited by kbluhm; 01-26-2012 at 03:33 PM.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •