php - Adding Ordinal Contractions to $i -


possible duplicate:
php display number ordinal suffix

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

somehow need last digit of $i test against if statements...

here code 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++;      } 

you can use modulus (%) operator remainder when dividing 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++;  } 

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -