EchoFool Posted June 18, 2009 Share Posted June 18, 2009 I have a function which changes numbers to a position like 1 = 1st 2 = 2nd etc etc But when the value 1 goes into the function it returns "1th". Not sure where it is that i got wrong in my logic... can you spot it ? <?php function ordinal($cdnl){ $test_c = abs($cdnl) % 10; $ext = ((abs($cdnl) %100 < 21 && abs($cdnl) %100 > 4) ? 'th' : (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1) ? 'th' : 'st' : 'nd' : 'rd' : 'th')); return $cdnl.$ext; } ?> Link to comment https://forums.phpfreaks.com/topic/162806-solved-function-for-number-strings/ Share on other sites More sharing options...
flyhoney Posted June 18, 2009 Share Posted June 18, 2009 Eek. Never never never nest ternaries. Even if it makes you feel cool. Link to comment https://forums.phpfreaks.com/topic/162806-solved-function-for-number-strings/#findComment-859114 Share on other sites More sharing options...
EchoFool Posted June 18, 2009 Author Share Posted June 18, 2009 Eek. Never never never nest ternaries. Even if it makes you feel cool. Pardon ? lol Link to comment https://forums.phpfreaks.com/topic/162806-solved-function-for-number-strings/#findComment-859121 Share on other sites More sharing options...
vbnullchar Posted June 18, 2009 Share Posted June 18, 2009 <?php function test($n) { if($n == '11' || $n == '12' || $n == '13' ){ $suff = 'th'; } elseif($n == '1'){ $suff = 'st'; } elseif($n == '2'){ $suff = 'nd'; } elseif($n =='3'){ $suff = 'ed'; } else { $suff = 'th'; } return $n . $suff; } ?> Link to comment https://forums.phpfreaks.com/topic/162806-solved-function-for-number-strings/#findComment-859126 Share on other sites More sharing options...
J.Daniels Posted June 18, 2009 Share Posted June 18, 2009 flyhoney is referring to the if/else shortcut: $value = (condition) ? true : false; I copied and pasted your code and it is working on my end. Link to comment https://forums.phpfreaks.com/topic/162806-solved-function-for-number-strings/#findComment-859127 Share on other sites More sharing options...
flyhoney Posted June 18, 2009 Share Posted June 18, 2009 <?php function ordinal($num) { $unit = $num % 10; if ($num >= 10 and $num <= 13) { $suffix = 'th'; } else { switch ($unit) { case 1: $suffix = 'st'; break; case 2: $suffix = 'nd'; break; case 3: $suffix = 'rd'; break; default: $suffix = 'th'; } } return $num.$suffix; } ?> Link to comment https://forums.phpfreaks.com/topic/162806-solved-function-for-number-strings/#findComment-859129 Share on other sites More sharing options...
EchoFool Posted June 18, 2009 Author Share Posted June 18, 2009 Thanks everyone Link to comment https://forums.phpfreaks.com/topic/162806-solved-function-for-number-strings/#findComment-859160 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.