Jump to content

[SOLVED] function for number strings


EchoFool

Recommended Posts

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

<?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;
}

?>

<?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;
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.