Skip to content

Instantly share code, notes, and snippets.

@Jeffrey04
Created December 4, 2012 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeffrey04/4202069 to your computer and use it in GitHub Desktop.
Save Jeffrey04/4202069 to your computer and use it in GitHub Desktop.
Count the number of digits of a number
#!/usr/bin/env php
<?php
function count_digits($number, $base = 10) {
return get_n(
$base,
intval(is_string($number) ? strval($number) : $number, $base)
);
}
function get_n($base, $number, $n = 1) {
return $number < pow($base, $n) ?
$n
: get_n($base, $number, $n + 1);
}
var_dump(count_digits(0xFFF, 16));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment