PHP Program to Count Digits of a Number Last Updated : 12 Dec, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will see how to count the digits of a number in PHP. There are three methods to count digits of a number, these are: Table of Content Using strlen() FunctionUsing while LoopUsing str_split() FunctionUsing strlen() FunctionThe strlen() function returns the length of a given string. It takes a string as a parameter and returns it's length. It calculates the length of the string including all the whitespaces and special characters. Syntax: strlen( $string );Example: PHP <?php $numStr1 = '110010'; $digitCount1 = strlen($numStr1); echo "String Length: " . $digitCount1; $number = 12345; $numStr2 = (string)$number; $digitCount2 = strlen($numStr2); echo "\nString Length: " . $digitCount2; ?> OutputString Length: 6 String Length: 5 Using while LoopIn this section, we use while loop to count the digits of a Number. First, we declare a counter variable and initialize with 0. Then check the given number is not equal to zero, then divide the number by 10, and increase the counter variable by 1. At last, return the counter variable that display the total digits of a number. PHP <?php function countDigits($number) { $digitCount = 0; while ($number != 0) { $number = (int)($number / 10); $digitCount++; } return $digitCount; } $numStr = '110010'; $digitCount1 = countDigits($numStr); echo "String Length: " . $digitCount1; $number = 12345; $digitCount2 = countDigits($number); echo "\nString Length: " . $digitCount2; ?> OutputString Length: 6 String Length: 5 Using str_split() FunctionThe str_split() function converts the given string into an array, and then use count() function to count total number of digits in given number. Example: PHP <?php function countDigits($number) { $digits = str_split($number); $digitCount = count($digits); return $digitCount; } $numStr = '110010'; $digitCount1 = countDigits($numStr); echo "String Length: " . $digitCount1; $number = 12345; $digitCount2 = countDigits((string)$number); echo "\nString Length: " . $digitCount2; ?> OutputString Length: 6 String Length: 5 Comment More infoAdvertise with us Next Article PHP | Sum of digits of a number B blalverma92 Follow Improve Article Tags : PHP Geeks Premier League 2023 Similar Reads PHP | Sum of digits of a number This is a simple PHP program where we need to calculate the sum of all digits of a number. Examples: Input : 711 Output : 9 Input : 14785 Output : 25 In this program, we will try to accept a number in the form of a string and then iterate through the length of the string. While iterating we will ext 1 min read PHP Program to Print ASCII Value of all Digits of a Given Number Given a number, the task is to print ASCII value of all digits of a given number in PHP. ASCII values are often used to represent characters and digits. Sometimes, it's necessary to find the ASCII value of each digit in a given number, especially when dealing with character encoding or data manipula 3 min read Write a program to display Reverse of any number in PHP ? Write a program to reverse the digits of an integer. Examples : Input : num = 12345 Output: 54321 Input : num = 876 Output: 678 It can be achieved using Iterative way or Recursive Way Iterative Method: Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by 2 min read PHP Program to Count set bits in an integer Write an efficient program to count number of 1s in binary representation of an integer. Examples : Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits Recommended: Please solve it on âPRACTICE 2 min read How to convert a String into Number in PHP ? Strings in PHP can be converted to numbers (float/ int/ double) very easily. In most use cases, it won't be required since PHP does implicit type conversion. This article covers all the different approaches for converting a string into a number in PHP, along with their basic illustrations.There are 4 min read Program to print multiplication table of any number in PHP In this article, we will see how to print the multiplication table of any given number using PHP. To make the multiplication table, first, we get a number input from the user and then use for loop to display the multiplication table. We use HTML and PHP to display the multiplication table. The HTML 1 min read Like