Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-22-2011, 12:07 AM   PM User | #16
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
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
Lamped is offline   Reply With Quote
Old 01-26-2012, 03:52 AM   PM User | #17
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
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';

jmj001 is offline   Reply With Quote
Old 01-26-2012, 02:55 PM   PM User | #18
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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"

__________________
ZCE

Last edited by kbluhm; 01-26-2012 at 03:33 PM..
kbluhm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:36 AM.


Advertisement
Log in to turn off these ads.