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; } ?> Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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; } ?> Quote Link to comment 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. Quote Link to comment 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; } ?> Quote Link to comment Share on other sites More sharing options...
EchoFool Posted June 18, 2009 Author Share Posted June 18, 2009 Thanks everyone Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.