note 129193 added to function.intval
| From: | DenesKellner at main dot php dot net | Date: | Wed, 24 Jan 2024 09:28:46 +0000 |
| Subject: | note 129193 added to function.intval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221147@lists.php.net to get a copy of this message | ||
Warning: intval() parses scientific notation, like "12.3e7".
This comes out of the blue when you use intval() to cut the first integer value from a string; at
first glance there's nothing wrong with it, if you have "123.jeff" it will give you
123, but in the rare case of parsing something that has a second segment with a hex number, you can
easily run into this. (Let's not start the "why would you parse a string like that"
argument.)
So if you're not prepared, these results may surprise you:
intval("123.ee-2") - gives you 123
intval("123.e2-e") - gives you 12300
intval("123.a2-e") - gives you 123
intval("123.e-22") - gives you 0
intval("123.e-a2") - gives you 123
intval("123.e-2a") - gives you 1
intval("123.2e2a") - gives you 12320
intval("123.22e2") - gives you 12322
intval("123.22ea") - gives you 123
Again, this is somewhat expected behaviour once you know that scientific notation is interpreted by
it. But it looks like a less-than-widely known fact and I only faced this issue after 20+ years of
php.
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 2001:4c4c:1924:8700:707e:9854:fa4a:950)
----
Manual Page -- https://php.net/manual/en/function.intval.php
Edit -- https://main.php.net/note/edit/129193
Del: integrated -- https://main.php.net/note/delete/129193/integrated
Del: useless -- https://main.php.net/note/delete/129193/useless
Del: bad code -- https://main.php.net/note/delete/129193/bad+code
Del: spam -- https://main.php.net/note/delete/129193/spam
Del: non-english -- https://main.php.net/note/delete/129193/non-english
Del: in docs -- https://main.php.net/note/delete/129193/in+docs
Del: other reasons-- https://main.php.net/note/delete/129193
Reject -- https://main.php.net/note/reject/129193
Search -- https://main.php.net/manage/user-notes.php