0

Possible Duplicate:
php display number with ordinal suffix

I'm attempting to add ordinal contractions i.e.(st/nd/rd/th) to an increment.

Somehow I need to get the last digit of $i to test it against my if statements...

Here is my code so far:

    $i = 1;
    while($i < 101 ){

    if($i == 1){$o_c = "st";}else{$o_c = "th";}
    if($i == 2){$o_c = "nd";}
    if($i == 3){$o_c = "rd";}

    echo $i.$o_c."<br/>";
    $i++;

    }
Community
  • 1
  • 1
AndrewFerrara
  • 2,313
  • 8
  • 28
  • 44

3 Answers3

0

What about using the modulus operator: $i % 10?

D.Shawley
  • 56,443
  • 9
  • 95
  • 111
0

Display numbers with ordinal suffix in PHP

(that thread has other solutions. I liked that one)

Community
  • 1
  • 1
AlfaTeK
  • 6,987
  • 14
  • 45
  • 88
0

You can use the modulus (%) operator to get the remainder when dividing by 10.

$i = 1;
while($i < 101 ){

$remainder = $i % 10;
if($remainder == 1){$o_c = "st";}else{$o_c = "th";}
if($remainder == 2){$o_c = "nd";}
if($remainder == 3){$o_c = "rd";}

echo $i.$o_c."<br/>";
$i++;

}
Alex Deem
  • 4,627
  • 1
  • 20
  • 24