function getLastDigitFromString($number) {
// Ensure the input is a string of digits (not a float or scientific notation)
if (!preg_match('/^\d+$/', $number)) {
throw new InvalidArgumentException("Input must be a string of digits.");
}
// Return the last character directly
return substr($number, -1);
}
try {
$bigInt = "123456789012345678901234567890123";
$lastDigit = getLastDigitFromString($bigInt);
echo "The last digit of $bigInt is: $lastDigit\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
/*
run:
The last digit of 123456789012345678901234567890123 is: 3
*/