Add suffix to a number
Jun 1, 2010 PHP Coding, Technology
Have you ever had to dynamically add the suffix to a number? (ie. st, nd, rd, th)
function number_suffix($i) { switch( floor($i/10) % 10 ) { default: switch( $i % 10 ) { case 1: return 'st'; case 2: return 'nd'; case 3: return 'rd'; } case 1: } return 'th'; }
Then to use the code you would do like:
$number = 10; // This will display 10th echo $number,number_suffix($number);