On Sat, 03 Oct 2009 11:57:36 -0400, f...@thefsb.org (Tom Worster) wrote:

>On 10/3/09 7:21 AM, "clanc...@cybec.com.au" <clanc...@cybec.com.au> 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

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

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

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 
convert real to integer.  Despite which if you print them without specifying a 
format it
will print them all as 2260.

All very messy!


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

Reply via email to