php get last digit of number 1

php get last digit of number

$number = 12356;
$lastDigit = $number % 10;
echo $lastDigit; // 6

Here is what the above code is Doing:
1. We create a variable called $number and assign it the value 12356.
2. We create a variable called $lastDigit and assign it the value of $number modulo 10.
3. We echo the value of $lastDigit.

Modulo is the remainder after division. For example, 10 modulo 3 is 1 because 10 divided by 3 leaves a remainder of 1.

In the above example, 12356 modulo 10 is 6 because 12356 divided by 10 leaves a remainder of 6.

Instructions
1.
Create a variable called $number and assign it the value 1234.

2.
Create a variable called $lastDigit and assign it the value of $number modulo 10.

3.
Echo the value of $lastDigit.

Similar Posts