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,383
  • 8
  • 30
  • 45

3 Answers3

0

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

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
0

Display numbers with ordinal suffix in PHP

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

Community
  • 1
  • 1
AlfaTeK
  • 7,487
  • 14
  • 49
  • 90
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,717
  • 1
  • 21
  • 24