13

I am working to count number of digit in PHP. I just want to count number of digit value for database.In first number have zero means , it won't take as a number for counting.

for example:

12  ==number of count value is 2
122 ==number of count value is 3

I can achieve this through function.here my function.

function count_digit($number)
{
    return strlen((string) $number);
}

$number = 12312;
echo count_digit($number); // 5

But I need to add zero for that number $num = 0012312; (zero-fill).

012 == number of count value is 3
0133 == number of count value is 4

Let me know how to solve it.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100

3 Answers3

30

If you wish the leading zeros to be counted too, you should assign it as a string and not as number.

Then, try to calculate the number of characters. This time, it will include the zeros. No need to type cast inside the function.

So, now your code will look like this:

function count_digit($number) {
  return strlen($number);
}

//function call
$num = "number here";
$number_of_digits = count_digit($num); //this is call :)
echo $number_of_digits;
//prints 5
Kevin
  • 6,539
  • 5
  • 44
  • 54
  • 2
    He should do this before passing the $number to the function, to retain the leading zeros. – Kevin Jun 11 '13 at 05:53
  • 3
    It's got to be in quotes at the time he assigns it or there is no hope... he's probably seeing all kinds of goofiness if he is not because php interprets numbers with a leading 0 as octal. – Orangepill Jun 11 '13 at 06:00
  • 1
    Hi @KevinPaladin...thank u for response me..its almost same as my function.what is difference bet. assign (string)$num and strlen((string) $number)... –  Jun 11 '13 at 06:03
  • sorry, edited the answer. You should never assign it to a variable as an integer. You should assign it only as a string. – Kevin Jun 11 '13 at 06:05
  • Hi @Orangepill....Thank u for response me..Please say how to include zero value by using that octal. –  Jun 11 '13 at 06:06
  • Hi @KevinPaladin...i just try it..when i gave `123456` means it result as `6` but i gave `0123456`number means it result as `5`.@KevinPaladin, how to rectify it... –  Jun 11 '13 at 06:10
  • It should work! Did u put the number within double quotes? can u post the assignment line that you used, here? – Kevin Jun 11 '13 at 06:13
  • 3
    @KevinPaladin..it working perfectly...sorrry i miss quotes for this.now it working perfectly...awesome answer...thank You...:-) –  Jun 11 '13 at 06:30
9
function count_digit($number) {
return strlen((string) $number);
}

//function call
$num = "012312";
$number_of_digits = count_digit($num); //this is call :)
echo $number_of_digits;

Make $num variable as a string.

Ganesh Kanawade
  • 381
  • 1
  • 4
  • 1
    Hi @Ganesh....I already gave a number as value include zero..it won`t work well...Let me explain it.thank u for response me. –  Jun 11 '13 at 06:00
0

There is no need for a function wrapper. The answer is

$n = "00001";
mb_strlen((string) $n);  // result is 5
  • There's no point of cast string to a string. And even if `$n` was an integer, the function would convert parameter to string anyways. So `mb_strlen(1.5)` will return 3. – Stichoza Feb 13 '22 at 00:43