Chris Andrews
asked on
Need to get last digit of number - what's most efficient way?
I have a value:
$overall = "598397"; //for example
I need to get the last digit of that.
I've done a bit of research and have found two solutions commonly bantered about:
$last_digit = $overall % 10;
and
$last_digit = substr($overall, -1);
Which is the most efficient way to do this?
$overall will always be positive, will be '0' sometimes, and can grow quite large (low millions possible),
Thanks for any thoughts on this,
Chris
$overall = "598397"; //for example
I need to get the last digit of that.
I've done a bit of research and have found two solutions commonly bantered about:
$last_digit = $overall % 10;
and
$last_digit = substr($overall, -1);
Which is the most efficient way to do this?
$overall will always be positive, will be '0' sometimes, and can grow quite large (low millions possible),
Thanks for any thoughts on this,
Chris
I'd probably use either of those. I think the results will usually be the same.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The question would be do you want the zero or a digit 1-9?
ASKER
I'd want 0 through 9 - looks like substr it is :)
Thank you both!
Chris
Thank you both!
Chris
ASKER
Thanks for running the test on these Ray -
Thanks for the points and thanks for using EE. I always find it's better to run a quick test than to depend on anything in the PHP funhouse :-) Happy New Year, ~Ray