PHP: intval, part 1: surprising output

Self-tutoring about PHP’s intval function: the tutor mentions a surprise.

I was reading about PHP’s intval function today. It’s meant, as I understand, to convert a decimal number to an integer, by just keeping the integer part. Putting it in comp sci terminology, intval(the_num) is supposed to truncate the_num rather than rounding it.

I found, at php.net, a post from 24 years ago by a user called zak at php dot net, wherein he points out that intval((0.1 + 0.7)*10) will likely give the answer 7, not 8.

I assumed that, since the post was from 24 years ago, a fix would likely have been applied to PHP since then. Yet curious, I tried it on a playground as well as on a server.

Some results I seemed to get were that intval((0.1 + 0.7)*10) gives 7, as zak said 24 years ago. However, intval((0.2 + 0.7)*10) gives 9.

Interesting, eh?

php-play.dev

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Leave a Reply