

Find First and Last Digits of a Number in PHP
source link: https://www.geeksforgeeks.org/find-first-and-last-digits-of-a-number-in-php/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Find First and Last Digits of a Number in PHP
Given a Number, the task is to find the first, and last digits of a number using PHP.
Examples:
Input: num = 145677
Output: First Digit: 1, and Last Digit: 7
Input: 101130
Output: First Digit: 1, and Last Digit: 0
There are three methods to get the first and last digit of number, these are:
Using Loop
In this section, we use while loop to get the first and last digit of number. Remainder operator is used to get the last digit of number, and for first digit, use while loop.
<?php function firstAndLastDigits( $number ) { $lastDigit = $number % 10; while ( $number >= 10) { $number = (int)( $number / 10); } $firstDigit = $number ; return [ $firstDigit , $lastDigit ]; } $num = 12345; list( $firstDigit , $lastDigit ) = firstAndLastDigits( $num ); echo "First Digit: $firstDigit, Last Digit: $lastDigit" ; ?> |
First Digit: 1, Last Digit: 5
Using Logarithm
Here, we use Logarithm to get the first digit of number. To get the first digit, divide the number by 10 power length of given number i.e. $number / pow(10, $len).
Example:
<?php function firstAndLastDigits( $number ) { $len = floor (log10( $number )); $firstDigit = (int)( $number / pow(10, $len )); $lastDigit = $number % 10; return [ $firstDigit , $lastDigit ]; } $num = 12345; list( $firstDigit , $lastDigit ) = firstAndLastDigits( $num ); echo "First Digit: $firstDigit, Last Digit: $lastDigit" ; ?> |
First Digit: 1, Last Digit: 5
Using str_split() Function
In this section, first, we convert the number of string and then split into an array, then get the first and last element of array.
<?php function firstAndLastDigits( $number ) { $arr = str_split ((string) $number ); $firstDigit = $arr [0]; $lastDigit = $arr [sizeof( $arr )-1]; return [ $firstDigit , $lastDigit ]; } $num = 12345; list( $firstDigit , $lastDigit ) = firstAndLastDigits( $num ); echo "First Digit: $firstDigit, Last Digit: $lastDigit" ; ?> |
First Digit: 1, Last Digit: 5
Recommend
-
30
circle visualization of a number's digits in Go Introduction In the image below, each dot represents a deci...
-
15
Yesterday, I was writing about collecting the mistakes that create interoperability issues in Web brow...
-
6
How to specify fractional digits for formatted number string in Swift Fractional digits are a number of digits after the decimal separator (.). When you want to present a floating...
-
6
Improve Article Reorder digits of a given number to make it a power of 2Difficulty Level : MediumLast Updated : 28 Jun, 2021...
-
7
Improve Article Rotate digits of a given number by KDifficulty Level : EasyLast Updated : 29 Apr, 2021Given two intege...
-
12
Validation of the telephone number not having all the digits repeated advertisements I want to validate a telephone number of foramt (xxx) xxx...
-
8
How to find K first digits of the decimal representation of 1 / N advertisements This is an interview question I came acros...
-
5
How To Generate A Valid Credit Card Number For A Bin (First 6 Digits) March 25, 2014 There is plenty of generators that can p...
-
8
C++ Program to Find the Sum of All Digits of a NumberC++ Program to Find the Sum of All Digits of a Number20 Views30/05/2022In this video,...
-
10
PHP Program to Count Digits of a Number ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK