Function to return number of digits of an integer in PHP

By: Iletras  

This is a very simple but useful function to return number of digits of an integer. 

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

//function call
$num = 12312;
$number_of_digits = count_digit($num); //this is call :)
echo $number_of_digits; 
//prints 5



Archived Comments

1. ekukela
View Tutorial          By: ekukela at 2017-09-06 14:33:01

2. ebootomuxo
View Tutorial          By: ebootomuxo at 2017-09-05 15:06:16

3. epoehoqo
View Tutorial          By: epoehoqo at 2017-09-05 14:15:19

4. Lorenton
View Tutorial          By: Lorenton at 2017-06-23 10:25:29

5. dcelneqezu
View Tutorial          By: dcelneqezu at 2017-01-08 05:07:51

6. ikeboite
View Tutorial          By: ikeboite at 2017-01-08 05:01:48

7. enuciluluzi
View Tutorial          By: enuciluluzi at 2017-01-08 04:46:18

8. if the number is for exemple 00025478 it return a wrong value :)
View Tutorial          By: ahmed at 2015-02-11 12:02:45

9. thanks. it helped a lot!
View Tutorial          By: lateforbus at 2013-04-25 03:35:24

10. Thanks.It is very helpful
View Tutorial          By: JENI at 2013-03-23 11:01:58

11. Great Thanks
View Tutorial          By: anawaz at 2012-10-05 10:28:20


Most Viewed Articles (in PHP )

Generate random timestamp between two dates

Building a Video Sharing Site using PHP in AWS

Installing PHP with nginx-server under windows

PHP 5.1.4 INSTALLATION on Solaris 9 (Sparc)

Building PHP 5.x with Apache2 on SuSE Professional 9.1/9.2

Installing PHP 5.x with Apache 2.x on HP UX 11i and configuring PHP 5.x with Oracle 9i

Cannot load /usr/local/apache/libexec/libphp4.so into server: ld.so.1:......

Setting up PHP in Windows 2003 Server IIS7, and WinXP 64

error: "Service Unavailable" after installing PHP to a Windows XP x64 Pro

Running different websites on different versions of PHP in Windows 2003 & IIS6 platform

Function to return number of digits of an integer in PHP

Function to sort array by elements and count of element in PHP

Function to force strict boolean values in PHP

Function to convert strings to strict booleans in PHP

Convert IP address to integer and back to IP address in PHP

Latest Articles (in PHP)

Comment on this tutorial