php get first digit of number

Solutions on MaxInterview for php get first digit of number by the best coders in the world

showing results for - "php get first digit of number"
Mirko
09 Apr 2018
1// in case the number is a string
2$s = '123';
3$a = $s[0];  
4echo $a; // 1
5
6// in case the number is an integer
7
8// Possibility 1
9$k = 123;
10$s = $k.''; // or: $s = (string)$k;
11$a = $s[0];  
12echo $a; // 1
13 
14// Possibility 2
15$k = 123;
16$a = floor($k/100);
17echo $a; // 1
18 
19// Possibility 3
20$k = 123;
21$a = substr($k, 0, 1); 
22echo $a; // 1
Sofia
13 May 2018
1$s = '123';
2$a = $s[0,1];  
3echo $a;
4
queries leading to this page
get 2 first digits of number phpphp find first number in stringget the first number phpget only first two digits of a number phpphp only keep first amount of numbers in stringphp variable first letter is numericphp keep only first to digit of a numberget first number of integer phphow to get first two digits of array in phpphp get first digit of numberonly first four number of strings phphow to get staring first digit value in phpget data only two digit in phpget the first digit of any number phphow to get all integer insted of first digit in phpget first 2 digits of number in phpget last digit of number phphow to get the first digit of a decimal in phpphp get first number of integerprint first ten natural number phpphp get first number from integerhow second number in first number by using phpget first digit phpget first number in numbers phpphp get first 2 digits from numberphp get first 10 numbersphp get last number from stringcheck what first number in int is phphow to get one digit in number in phphow second number in first number ny using phpphp get first number of intget first digit of integer phpphp only grab first 5 digits of stringphp get first 2 cnumbersget first digit of number phpget 2digit number in laravelhow to only select the first two numbers phphow to get first digit of a number in phpget first 5 numbers from int phppick only first three digits number phpphp get first digit in veriablephp only take the first 10 digitsphp first number in numberget the first two digit of a number phpget the first digit from an integer in phpget first digit and alphabet in phpcheck first character string of integer php first digit phpget first 4 digits of string phpget first 2 digits of number phpphp first number of a numberfirst digit in number phpgetting the first 6 digits of a number in phphow to get the first number of an integer in phphow to print first 5 digit number of 16 649323621228 in phpget first digit of string phpphp check if the first char is number phpget first 4 number in string phpphp 10 first digitcheck first number phphow to get the first digit of a number in phpphp return first element of arrayget first 4 digits of a number phpphp get first digit of number