How to count the number of digits to zero in php

I work to count the number of digits in PHP. I just want to count the number of digits for the database. The first issue has zero funds, it does not take as a number to count.

, eg:

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

I can achieve this through function.here is my function.

 function count_digit($number) { return strlen((string) $number); } $number = 12312; echo count_digit($number); // 5 

But I need to add zero for this 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.

+7
source share
2 answers

If you want the leading zeros to be counted as well, you should assign it as a string, not as a number.

Then try to calculate the number of characters. This time it will include zeros. No need to enter text inside a 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 
+14
source
 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 the variable $ num as a string.

+6
source

All Articles