> Le 27 mars 2019 à 00:09, Benjamin Morel <benjamin.mo...@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


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to