Re: [RFC] Saner string to number comparisons
| From: | Gabriel Caruso | Date: | Wed, 27 Feb 2019 14:00:42 +0000 |
| Subject: | Re: [RFC] Saner string to number comparisons | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-104532@lists.php.net to get a copy of this message | ||
Em ter, 26 de fev de 2019 às 09:28, Nikita Popov <nikita.ppv@gmail.com>
escreveu:
> Hi internals,
>
> I think it is well known that == in PHP is a pretty big footgun. It doesn't
> have to be. I think that type juggling comparisons in a language like PHP
> have some merit, it's just that the particular semantics of == in PHP make
> it so dangerous. The biggest WTF factor is probably that 0 == "foobar"
> returns true.
>
> I'd like to bring forward an RFC for PHP 8 to change the semantics of ==
> and other non-strict comparisons, when used between a number and a string:
>
> https://wiki.php.net/rfc/string_to_number_comparison
>
> The tl;dr is that if you compare a number and a numeric string, they'll be
> compared as numbers. Otherwise, the number is converted into a string and
> they'll be compared as strings.
>
> This is a very significant change -- not so much because the actual BC
> breakage is expected to be particularly large, but because it is a silent
> change in core language semantics, which makes it hard to determine whether
> or not code is affected by the change. There are things we can do about
> this, for example the RFC suggests that we might want to have a transition
> mode where we perform the comparison using both the old and the new
> semantics and warn if the result differs.
>
> I think we should give serious consideration to making such a change. I'd
> be interested to hear whether other people think this is worthwhile, and
> how we could go about doing it, while minimizing breakage.
>
> Regards,
> Nikita
>
Hi Nikita,
There's only one case that I'm not sure about
> Comparison | Before | After
> ------------------------------
> 0 == "" | true | false
I know that a cast of am empty and non-empty string results into 0 (
https://3v4l.org/W0P3Z), but still, not sure about this one in
specific.
Best,
--
Gabriel Caruso