@so_is_thisDec 28.2006 — #If your list will have less than 32 entries, use [B]date("S")[/B] and you won't need all that code.[/QUOTE] "...all that code," yeah, right. The array, for sure. But, say, why don't you give an example of using that function to achieve such results on the numbers from 1 to 31. Let's see if you'll save on "...all that code." Teach me, please. Thanks. ?
@so_is_thisDec 28.2006 — #Perhaps you didn't believe me when I said, "Teach me, please." I meant it. ? So, I taught myself. Result? Both of the following yield the same result for the numbers from 1 through 31: [code=php] <pre><?php $endings = array('th','st','nd','rd','th','th','th','th','th','th'); for ($x=1; $x<=31; ++$x) { echo $x . $endings[($x<11||13<$x)?($x%10):0] . " ...n"; } ?></pre>[/code] [code=php] <pre><?php for ($x=1; $x<=31; ++$x) { echo $x . date('S', mktime(0,0,0,1,$x,2000)) . " ...n"; } ?></pre>[/code] So, does using the [B]date()[/B] function truly save on "...all that code"? I don't think so. Only an array is saved, but me thinks it takes more to execute those two functions repeatedly (in the second example) than it does to reference an array which is built just once. On the other hand, though the first example is not restricted to a particular range of numbers, the second example does not require one to come up with the correct algorithm for extracting such endings from an array. ?
@NogDogDec 28.2006 — #Don't know why, I just felt like using the date('S') thing but making it work for any integer, positive or negative. This is the function I came up with -- and that I doubt I'd ever actually use. ? [code=php] <?php function get_ordinal_suffix($int) { settype($int, "integer"); $int = abs($int); while($int > 31) { $int -= 10; } return $int === 0 ? "th" : date('S', mktime(1,1,1,1,$int)); } // TEST IT: header("Content-Type: text/plain"); for($ix = -101; $ix <= 101; $ix++) { echo "$ix".get_ordinal_suffix($ix)."n"; } ?> [/code]