Find first and last digits of a number | Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; Driver Code
< ? php function firstDigit ( $ n ) { while ( $ n >= 10 ) $ n /= 10 ; return ( int ) $ n ; } function lastDigit ( $ n ) { return ( ( int ) $ n % 10 ) ; } $ n = 98562 ; echo firstDigit ( $ n ) . " ▁ " . lastDigit ( $ n ) . " STRNEWLINE " ;