Link to home
Start Free TrialLog in
Avatar of Chris Andrews
Chris AndrewsFlag for United States of America

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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I'd probably use either of those.  I think the results will usually be the same.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of North2Alaska
The question would be do you want the zero or a digit 1-9?
Avatar of Chris Andrews

ASKER

I'd want 0 through 9 - looks like substr it is :)

Thank you both!

Chris
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