How to use str_split() Function In PHP
The 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 ; ?> |
Output
String Length: 6 String Length: 5
PHP Program to Count Digits of a Number
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() Function
- Using while Loop
- Using str_split() Function