Re: A really wacky design decision

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 03 Oct 2009 11:57:36 -0400, fsb@xxxxxxxxxx (Tom Worster) wrote:

>On 10/3/09 7:21 AM, "clancy_1@xxxxxxxxxxxx" <clancy_1@xxxxxxxxxxxx> wrote:
>
>> However there is one feature of PHP which, to my mind, is really bad design.
>> How many of
>> you can see anything wrong with the following procedure to search a list of
>> names for a
>> particular name?
>> 
>> $i = 0; $j = count ($names); while ($i < $j)
>> { if ($names[$i] == $target) { break; }
>> ++$i;
>> }
>> 
>> As long as the names are conventional names, this procedure is probably safe
>> to use.
>> However if you allow the names to be general alphanumeric strings, it is not
>> reliable. One
>> of my programs recently broke down in one particular case, and when I
>> eventually isolated
>> the bug I discovered that it was matching '2260' to '226E1'. (The logic of
>> this is: 226E1
>> = 226*10^1 = 2260).
>> 
>> I agree that I was well aware of this trap, and that I should not have used a
>> simple
>> comparison, but it seems to me to be a bizarre design decision to assume that
>> anything
>> which can be converted to an integer, using any of the available notations, is
>> in fact an
>> integer, rather than making the default to simply treat it as a string.
>
>this is odd.
>
>i might think it ok for (2260 == '226E1') to be true since php would be
>doing type juggling in a logical left-to-right manner: we start with an
>integer 2260, next is the juggling comparison operator, then a string, so it
>might reasonably try to convert the string to an integer and then compare.
>
>but with ('2260' == '226E1'), where both lhs and rhs are already of the same
>time, it seems elliptical, to say the least, that php should search the
>types to which it can convert the strings looking for one in which they are
>equal.

The order doesn't matter; 2260 == 226e1, and 226e1==2260.

It looks as if (for comparisons) PHP's order of preference is Integer > Real > ?? > ?? >
String.

If you use '==' it will try to convert everything to integer, but if you use '===' it will
try to render them in some standard format (so that 226e1 === 2.26e3), but will not
convert real to integer.  Despite which if you print them without specifying a format it
will print them all as 2260.

All very messy!

Clancy

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux