Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
My very first post
I wonder what possibilities there are to get the middle digit of a number. Eg: 439 -> 3
I could do it with string functions but I'd like to handle the number as an integer and not as a string. Is there maybe a mathematical approach to do this? I heard something and I became curious.
function getDigit($num, $digit) {
return intval($num/pow(10,$digit-1))%10;
}
echo getDigit('3456', 3);
That's getting the nth digit (specified by the second argument). You'd need to work out the length of the number to get the middle digit. Obviously you'd normally use strlen() for that...
onion2k wrote:That's getting the nth digit (specified by the second argument). You'd need to work out the length of the number to get the middle digit. Obviously you'd normally use strlen() for that...
Ah, you're right. I misunderstood what the OP was looking for.