PHP string functions question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
klewman
Forum Newbie
Posts: 1
Joined: Wed Jun 20, 2007 2:44 pm

PHP string functions question

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

substr()
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You could also do a %10.

Code: Select all

$lastDigit = $number % 10;
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Doesn't this belong in the PHP Code forum?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

You could also do a %10.
Hmm...

Code: Select all

$number = '+38(095) 123-1139';
$lastDigit = $number % 10; // $lastDigit = WHAT?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Doesn't this belong in the PHP Code forum?
Indeed. Moved.
Post Reply