Re: wat is this for a logic
| From: | Lars Torben Wilson | Date: | Mon, 30 Oct 2000 23:22:03 +0000 |
| Subject: | Re: wat is this for a logic | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-22777@lists.php.net to get a copy of this message | ||
Norbert Pfeiffer writes:
> 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.
>
> Please answers me personally, because I am not entered in the list.
>
> Thank you for your trouble with the response.
>
>
> With best greetings
>
> Norbert Pfeiffer
> _____________________________________
> www.uris.de pfeiffer@uris.de
> 0177-2363368 02292-681769
> -------------------------------------
> e.o.m.
Perhaps I should add this to the FAQ. :)
The reason is that when you use the == operator, PHP automatically
converts the operands (the things you are comparing) to compatible
types--in this case, the 0 remains an integer, and is compared against
the _integer_ value of "foo".
Now, when PHP converts a string to an integer, the result is
calculated from the value of any digit characters in the string up to
the first non-digit character. For instance, PHP would convert the
string "23foo" to the integer 23, and so on. Since the string "foo"
has no digits before the first non-digit, the resulting integer is 0.
In other words, when you write 0 == "foo", PHP sees 0 == 0. If you
did 0 == "23foo", PHP would see 0 == 23. And so on.
PHP 4 includes a new operator: ===. This means 'equal and of the same
type'. So 0 === "foo" would evaluate as false, since although 0 and
"foo" have the same integer values, they are not actually both of the
same type.
For more information on these conversions, read:
http://www.php.net/manual/de/language.types.type-juggling.php
http://www.php.net/manual/de/language.types.string.php#language.types.string.conversion
Hope this helps explain it,
Torben
--
+----------------------------------------------------------------+
|Torben Wilson <torben@php.net> Netmill iTech|
|http://www.coastnet.com/~torben http://www.netmill.fi|
|Ph: 1 250 383-9735 torben@netmill.fi|
+----------------------------------------------------------------+