How to recursively count the digits of a number in PHP

1 Answer

0 votes
function count_digits($n) {
    static $counter = 0;
 
    if ($n != 0) {
        $counter++;
        count_digits((int)$n/10);
    }
 
    return $counter;
}
   
$n = 681359;
     
echo count_digits($n);


   
/*
run:
        
7
       
*/

 



answered Apr 5, 2019 by avibootz

Related questions

1 answer 162 views
1 answer 161 views
1 answer 158 views
1 answer 157 views
1 answer 174 views
1 answer 150 views
1 answer 176 views
176 views asked Oct 13, 2016 by avibootz
...