Re: wat is this for a logic

From: Date: Tue, 31 Oct 2000 00:00:22 +0000
Subject: Re: wat is this for a logic
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-22783@lists.php.net to get a copy of this message
Norbert Pfeiffer wrote: > > Hallo, > > please my bad excuses English, I assigned babelfish the translation :) > > My question: > see here http://itbw.de/_tests/diverse/check_ext.php3 > > Why is always true (0 == "foo") ? That is not logical for me. > > I wrote this problem 3 time into the German mailing list, > but nobody could explain or wanted it to me. The simple answer is, that's the way it is, so who cares why? It's arbitrary. The arbitrary answer is, "foo" is cast to an integer, which is 0 (all strings not beginning with a number are cast to 0 in a conversion). Yes, there are other ways you could do it, but they would be arbitrary as well (though casting the '0' into a string would make more sense to me). Easy solution: if not sure if you will be comparing apples and oranges, put quotes around your vars, so if $x = 0; and $y = "foo"; try "$x" == "$y" but personally I think it's sloppy programming if you don't know the type of your vars . . . you're bound to screw up somewhere. I always check user input and cast it to the type I expect it to be explicitly so I can have a default value that's reasonable (often that's not 0 but -1). See http://php.net/manual/html/language.types.html on type casting and http://php.net/manual/html/language.types.type-juggling.html on automatic type conversions and http://php.net/manual/html/language.types.string.html#language.types.string.conversion more to the point of your question, although it still doesn't answer why the cast is to integer instead of string. Andreas Pour http://www.kde.com/ : Everything KDE http://apps.kde.com/: The Latest in KDE Applications

« previous php.general (#22783) next »