Re: [RFC] Warning for implicit float to int conversions
| From: | Benjamin Morel | Date: | Thu, 04 Feb 2021 17:54:52 +0000 |
| Subject: | Re: [RFC] Warning for implicit float to int conversions | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-113078@lists.php.net to get a copy of this message | ||
On Thu, 4 Feb 2021 at 17:05, G. P. B. <george.banyard@gmail.com> wrote:
> Greetings internal,
>
> I'm proposing a new RFC which would warn when an implicit conversion
> from float to int occurs.
>
> The draft is currently located on GitHub:
> https://github.com/Girgias/float-int-warning/
> for ease of commenting/providing changes to it.
>
> The official discussion phase wouldn't start before I convert it to docwiki
> and
> post it on the wiki, something I'm planning to do next week.
>
> Any feedback is appreciated.
>
> Best regards,
>
> George P. Banyard
>
Hi George,
Thank you for this proposal, I'm all for it, though I'd go one step
further, and actually issue a warning during explicit casting as well:
(int) 3.5; // E_WARNING
(int) round(3.5); // OK
In my experience, it's better to be explicit about your intent, so forcing
the user to round() before casting when the float has a fractional part is
OK to me.
This would help prevent weird silent conversions such as when you cast to
(int) from user input:
$age = $_GET['age']; // '25.75'
$x = (int) $foo; // I'd expect a warning, not a silent conversion to 25
— Benjamin