Re: [RFC] Permit trailing whitespace in numeric strings
| From: | Claude Pache | Date: | Wed, 27 Mar 2019 10:28:17 +0000 |
| Subject: | Re: [RFC] Permit trailing whitespace in numeric strings | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-104963@lists.php.net to get a copy of this message | ||
> Le 27 mars 2019 à 00:09, Benjamin Morel <benjamin.morel@gmail.com> a écrit :
>
> - I would also strongly suggest that 2 strings are always compared
> byte-by-byte; IMO it does (*somehow*) make sense that 42 == "42.0", but it
> DOES NOT make sense that "42" == "42.0". (...)
There are many ways to obtain numeric data in the form of a string from external sources (HTML form,
database, CSV, ...) and even internal sources (bcmath, ...)—and note that, even when such
numerical data may have a decimal part, it does *not* mean that they are imprecise. It is a
well-known feature that you can work with such data without previously converting it explicitly to
some number type. That works pretty well in PHP (well, except that you get intermittent obscure bugs
when some third-party code in the same thread plays with setlocale(), but that's another
story...) If we cease to compare numeric strings numerically, I expect that much code will begin to
break more or less subtly, and will need to be thoroughly reviewed in order to be corrected. We must
not change that.
The core issue is that “==” is fundamentally ambiguous: should it compare as number, as string,
as reference, ...? If we were to redesign PHP from scratch, we may decide other semantics than the
current one (e.g., use distinct operators for comparing numerically and textually, as does Perl).
But where we are now, the best thing to do is to follow the common wisdom: If you want to compare
strings as strings, just use “===”. Or strcmp.
—Claude