Php log and floor function strange behavior

Home / Uncategorized / Php log and floor function strange behavior

Question:
My problem is the following: log(1000,10) returns 3, but floor(log(1000,10)) returns 2. How to fix this issue?

My phpversion is 5.6.30-0+deb8u1.


Answer:
From a 13 year old comment at the PHP:log documentation:$val = 1000000
$val2 = floor(log($val,10)) // gives a value of 5 for $val2 and not 6 as expected.
$val2 = floor(log10($val)) // gives the correct value.

So you should use floor(log10(1000);

While I’m certainly no expert, I think the different outcome in PHP5 and PHP7 (as pointed out in the comments on your question) has to do with scalar type declaration, a new feature in PHP 7 (try playing with strict mode to find out more, eg. https://3v4l.org/KVCol).
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *