PHP snippet : Number of digits of an integer

This is a basic PHP program that returns the number of digits of an integer.

[php]
<?php
//This is a simple function to return number of digits of an integer.

//function declaration
function count_digit($number)
{
$digit = 0;
do
{
$number /= 10; //$number = $number / 10;
$number = intval($number);
$digit++;
}while($number!=0);
return $digit;
}

//function call
$num = 12312;
$number_of_digits = count_digit($num); //this is call 🙂
echo $number_of_digits;
//prints 5
?>
[/php]

Mohit Kumar

A simple person who likes to share his thoughts on this blog. Know more about me

You may also like...

1 Response

  1. Alan says:

    or what about strlen( (string) $num );

Leave a Reply

Your email address will not be published. Required fields are marked *