Re: [PHP-DEV] [RFC] Binary String Comparison

[email protected] (Tjerk Meesters)
Newsgroups php.internals
Message-ID <CAHMUw2GMKrV3pzJuarYcKYK6cmCK5BM7MLXse__1VUs0Tv2G3w@mail.gmail.com>
On Tue, Aug 19, 2014 at 11:07 AM, Tjerk Meesters <[email protected]>
wrote:

>
> On Mon, Aug 18, 2014 at 11:30 PM, Johannes Schlüter <
> [email protected]> wrote:
>
>> On Mon, 2014-08-18 at 23:09 +0800, Tjerk Meesters wrote:
>> > > On 18 Aug, 2014, at 10:47 pm, Johannes Schlüter <
>> [email protected]> wrote:
>> > >
>> > >> On Mon, 2014-08-18 at 14:44 +0200, Marc Bennewitz wrote:
>> > >> The question isn't "What's wrong with ===, strcmp()?" but "What's
>> wrong
>> > >> with ==, <, >?".
>> > >>
>> > >> We have a standard way to compare two operands but currently we do
>> some
>> > >> magic things to solve something that don't need to be solved.
>> > >
>> > > Still it is a key property of the language which we can't simply
>> change.
>> > > Also mind this: All input data are strings and some databases also
>> > > return data as string. So code like
>> > >
>> > >   if ($_GET['id'] > 0)
>> > > or
>> > >   if ($db->fetchRow()[0] == 12)
>> > >
>> > > which is common will break.
>> >
>> > Those two cases will actually not be affected, it's strictly
>> string<=>string comparisons that's being discussed here.
>>
>> Meaning that simple code you find everywhere, in every second tutorial
>>
>>    foreach ($db->query("SELECT id, title FROM entries") as $row) {
>>        echo "<tr><td";
>>        if ($row[0] == $_GET['highlight_id']) {
>>            echo " background='#ff0000'";
>>        }
>>        echo ">".htmlentities($row[1])."</td></tr>";
>>    }
>>
>> will suddenly fail. How wonderful! (irony)
>>
>
> Not necessarily and certainly not by definition; reasons for failure are
> less obvious such as (but not limited to):
>
> "0" == "0.0"
> "11" == " 11" (but note that "11" == "11 " currently yields false)
> "0" == ""
>

I had mistakenly assumed that "0" == "" would currently yield true, but it
doesn't. My apologies for that. The other two statements are still valid,
though. So are these:

"0" == "0x0"
"0" == "00"


> I'm not arguing for or against this behaviour change, but I found it
> necessary to clear up some apparent confusion as to what repercussions this
> proposal carries.
>
> Another approach of attempting to solve the common issue of comparing big
> numbers with '==' is to only enforce string-wise comparison if a number
> cast would cause precision loss.
>
>
>> johannes
>>
>> ps. yes, the example might be done nicer and better, it still represents
>> a common pattern.
>>
>>
>
>
> --
> --
> Tjerk
>



-- 
--
Tjerk
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.