Re: Zero is equal to any string?

From: Date: Sun, 03 Sep 2000 09:27:20 +0000
Subject: Re: Zero is equal to any string?
Groups: php.general 
Request: Send a blank email to php-general+get-14969@lists.php.net to get a copy of this message
Addressed to: "Simon Garner" <sgarner@expio.co.nz> php-general@lists.php.net ** Reply to note from "Simon Garner" <sgarner@expio.co.nz> Sun, 3 Sep 2000 18:20:34 +1200 > > Hi, > > I encountered some rather baffling behaviour today while working on a > page, and eventually narrowed the problem down to the following: > > <? if (0 == "some string") { echo "They're equal!"; } ?> > > This will echo "They're equal!", which makes no sense to me. Why > should 0 be equal to any string? 0 should surely be equal only to the > empty string and the boolean value false. > > Note that (1 == "some string") returns false, and so does (0 === "some > string") as expected. I've tested the (0 == "some string") under > 4.0.2, 4.0.1pl2 and 3.0.16 always with the same results. > > Is this a bug or a "feature" of the language? > Feature. Any string when evaluated in a numeric context has a value of 0. Try ( 0 === 'some string' ), which may work or may always be false, if it is always false then try ( '0' === 'some string' ) which should be true if some string is '0'. See: http://www.php.net/manual/html/language.operators.comparison.html NOTE: === is php4 only. If you are not yet using php 4, you should seriously consider it now. I've been lurking on the developers list and belive 4.0.2 to be the best debugged version of PHP ever! Rick Widmer Internet Marketing Specialists www.developersdesk.com

« previous php.general (#14969) next »