{"id":49125,"date":"2025-04-08T18:25:07","date_gmt":"2025-04-08T18:25:07","guid":{"rendered":"https:\/\/www.oracletutoring.ca\/blog\/?p=49125"},"modified":"2025-04-08T18:25:08","modified_gmt":"2025-04-08T18:25:08","slug":"php-intval-part-1-surprising-output","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/php-intval-part-1-surprising-output\/","title":{"rendered":"PHP: intval, part 1: surprising output"},"content":{"rendered":"\n<h2>Self-tutoring about PHP&#8217;s intval function: the tutor mentions a surprise.<\/h2>\n<p>\nI was reading about PHP&#8217;s intval function today. It&#8217;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.<\/p>\n<p>\nI found, at <a href=\"https:\/\/www.php.net\/manual\/en\/function.intval.php\">php.net<\/a>, 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.<\/p>\n<p>\nI 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.<\/p>\n<p>\nSome 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.<\/p>\n<p>\nInteresting, eh?<\/p>\n<p>\n<a href=\"https:\/\/php-play.dev\/?c=DwfgDgFmBQD0sAICmAPAhgWzAGyQgxgPYAmS0kYAlgHYBmhAFAJQDcQA&#038;v=8.4&#038;f=html\">php-play.dev<\/a><\/p>\nJack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p> \n","protected":false},"excerpt":{"rendered":"<p>Self-tutoring about PHP&#8217;s intval function: the tutor mentions a surprise. I was reading about PHP&#8217;s intval function today. It&#8217;s meant, as I understand, to convert a decimal number to an integer, by just keeping the integer part. Putting it in &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/php-intval-part-1-surprising-output\/\"> <span class=\"screen-reader-text\">PHP: intval, part 1: surprising output<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2955],"tags":[3613,3614],"class_list":["post-49125","post","type-post","status-publish","format-standard","hentry","category-php","tag-cast","tag-float-to-integer"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/49125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=49125"}],"version-history":[{"count":7,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/49125\/revisions"}],"predecessor-version":[{"id":49132,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/49125\/revisions\/49132"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=49125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=49125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=49125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}