The accepted answer will not work with large numbers. The best way to calculate the length of any number is to call floor(log10($num) + 1) with a check for 0 .
$num = 12357; echo $num !== 0 ? floor(log10($num) + 1) : 1;
It has several advantages. It is faster, you do not perform type conversion, it works with large numbers, it works with various number systems, such as bin, hex, oct.
The equation makes a logarithm with base 10, then is half and adds 1.
This solution can work regardless of the base, so if you want to calculate the length of the binary or hexadecimal code, just change the base of the logarithm.
Working violin
source share