Re: A really wacky design decision

From: Date: Sat, 03 Oct 2009 17:49:53 +0000
Subject: Re: A really wacky design decision
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-298534@lists.php.net to get a copy of this message
On 10/3/09 12:25 PM, "Ashley Sheridan" <ash@ashleysheridan.co.uk> wrote: > On Sat, 2009-10-03 at 11:57 -0400, Tom Worster wrote: > >> On 10/3/09 7:21 AM, "clancy_1@cybec.com.au" <clancy_1@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 >> equal. >> > > I don't know what you mean by elliptical, but I'd expect and certainly > hope that PHP wouldn't try to convert both strings to something which > would mean they had the same value. Also, afaik, if the variables types > are exactly the same, PHP won't try to convert either of them for a > comparison i once had such hope too. $ php -r "var_dump('2260' == '226E1');" bool(true) elliptical: tending to be ambiguous, cryptic, or obscure: an elliptical prose that is difficult to translate. (http://dictionary.reference.com/browse/elliptical)

« previous php.general (#298534) next »