00 Votes

PHP: Determine last digit of a number

Question by Guest | 2014-02-10 at 20:09

In one of my PHP scripts, I have to extract the last digit/figure from an integer value.

For example, given the number 231, I would like to get the cypher "1".

Is there any PHP function for this?

ReplyPositiveNegative
0Best Answer0 Votes

To determine the last digit of a number, you can just use the modulo operator % like this:

$k = 231;
$z = $k % 10;

The modulo operator returns the remainder of a division. So, 231 divided by 10 are 230 + a remainder of 1. And 1 is your required last figure.
2014-02-10 at 23:21

ReplyPositive Negative
Reply

Related Topics

PHP: Determine Week Number

Tutorial | 0 Comments

What are IBAN and BIC?

Info | 0 Comments

PHP: Current Date and Time

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.