Re: How can an elephant count for nothing?
| From: | Per Jessen | Date: | Thu, 12 Feb 2009 10:55:23 +0000 |
| Subject: | Re: How can an elephant count for nothing? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-288146@lists.php.net to get a copy of this message | ||
Clancy wrote:
> While PHP has a lot of nice features, it also has some traps which I
> am forever falling into. One which I find particularly hard to
> understand is how mixed mode comparisons work. For instance
>
> $string = 'elephant';
> If($string == 0) returns true;
> If($string != 0) returns false;
> If($string === 0) returns false;
>
> I know that in this case I should use 'If($string == '')', but I still
> manage to forget. Can anyone explain clearly why comparing a string
> with zero gives this apparently anomalous result?
I'm not certain, but I suspect it's because the interpreter attempts to
convert "elephant" to an integer first.
/Per
--
Per Jessen, Zürich (-0.6°C)