I'm pretty sure (though it's very possible I'm wrong) that PHP doesn't have anything built in to do this outside of dates and I searched the internet and couldn't find anything worth a darn via google.
PHP Code:
#Adds st nd rd th to appropriate numbers function number_ending ($number) { // Get the last character of a string. $str = $number; $string = $str[strlen($str)-1]; $j = 0;
<?php
function show_ordinal($num) {
// first convert to string if needed
$the_num = (string) $num;
// now we grab the last digit of the number
$last_digit = substr($the_num, -1, 1);
// if the string is more than 2 chars long, we get
// the second to last character to evaluate
if (strlen($the_num)>1) {
$next_to_last = substr($the_num, -2, 1);
} else {
$next_to_last = "";
}
// now iterate through possibilities in a switch
switch($last_digit) {
case "1":
// testing the second from last digit here
switch($next_to_last) {
case "1":
$the_num.="th";
break;
default:
$the_num.="st";
}
break;
case "2":
// testing the second from last digit here
switch($next_to_last) {
case "1":
$the_num.="th";
break;
default:
$the_num.="nd";
}
break;
// if last digit is a 3
case "3":
// testing the second from last digit here
switch($next_to_last) {
case "1":
$the_num.="th";
break;
default:
$the_num.="rd";
}
break;
// for all the other numbers we use "th"
default:
$the_num.="th";
}
// finally, return our string with it's new suffix
return $the_num;
}
?>
same like what you post in #1, I also mess it and everything after 3 ends in th.
I fix this in #8. I don't know which is faster, mine is obvious shorter.
I'm very new to PHP and programming in general. It's becoming increasingly clearer that there is more than one way to do just about anything and ultimately it comes down to using the most efficient method available.
The question then is how we know which way is the most efficient.
Here we have 3 functions that all do the same thing different ways, which one works the best though?
Thats trickier to answer. When it comes to algorithms, most of them can be calculated by an order of magnitude. Since these all have an O(1) magnitude, they are all technically as efficient from a magnitude view. Magnitude really only cares about the 'big' picture as a whole.
So, look at other factors. Datatypes are my primary concern. If you treat you're number as a string, you will waste time and resource, but looking at the last digit itself is a matter of a char lookup. So, should the number be interpreted as a string, any string > 4 characters in length has exceeded the memory size required for any number storable as an integer (and this will increase in PHP6 which is going to native unicode).
kbluhm and oesxyl have both ensured that they are working with numbers. Oesyxl's approach is slightly more efficient, but will not tell you if its actually a number (and therefore becomes untrappable). It doesn't care if you give it a string, if you do it will cast it to a number. I believe that the is_* functions are slightly less efficient than direct casting. The benifit is you can toss and trap errors associated with this approach.
Though the codes are negligable, efficiency wise I expect that oesxyl's will be slightly faster in execution time; however, like I mentioned there is little that can be done to trigger / throw and trap any errors from it since it technically cannot have any errors with the explicit casting. Oesxyl's appears to also waste a little more memory than kbluhm's method since it declares two local variables (since the third is a write-on-copy context).
Weight on weight, I'll bet if I tested these to benchmark on precision to 1000, they would come up identical. At 100000, I'd suspect to see a 5 - 8 variance, which is incredibly tiny.
Also, one more factor is readability. Efficiency is IMO tossable for readability in languages like PHP. We're not doing embedded microprocessing, so memory and cpu are not really an issue nowadays. You can certainly take a more readable approach so you can understand later what you've done without needing to walk through it step by step (from the above, kbluhm's is more readable than oesxyl's).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
I'm very new to PHP and programming in general. It's becoming increasingly clearer that there is more than one way to do just about anything and ultimately it comes down to using the most efficient method available.
The question then is how we know which way is the most efficient.
Here we have 3 functions that all do the same thing different ways, which one works the best though?