Page 1 of 1

PHP string functions question

Posted: Wed Jun 20, 2007 2:50 pm
by klewman
How would I get PHP to read the only the last digit of a number?

I want to insert a number, have PHP read the last digit of it, and then take a specific action depending on that last digit. I looked through the string functions list on php.net but had no luck finding what I was looking for.

If anyone knows the function I'm trying to find, please post it. I appreciate all your help.

Posted: Wed Jun 20, 2007 2:52 pm
by Weirdan
substr()

Posted: Wed Jun 20, 2007 4:37 pm
by superdezign
You could also do a %10.

Code: Select all

$lastDigit = $number % 10;

Posted: Wed Jun 20, 2007 4:39 pm
by JellyFish
Doesn't this belong in the PHP Code forum?

Posted: Wed Jun 20, 2007 4:39 pm
by Weirdan
You could also do a %10.
Hmm...

Code: Select all

$number = '+38(095) 123-1139';
$lastDigit = $number % 10; // $lastDigit = WHAT?

Posted: Wed Jun 20, 2007 4:40 pm
by Weirdan
Doesn't this belong in the PHP Code forum?
Indeed. Moved.