note 88950 added to language.types.string
| From: | alan at allgo dot com | Date: | Sat, 14 Feb 2009 21:54:06 +0000 |
| Subject: | note 88950 added to language.types.string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150684@lists.php.net to get a copy of this message | ||
This is an interesting scenario:
If I pass:
$var1 = 0;
if($var1=="some_string")
{
echo "this returns true only for 0";
}
How can it return true when comparing an integer of 0 to any string?
My guess is that its reading 0 as false and seeing the string as a "false" integer: hence,
false == false.
Am I right?
The solution is to convert the integer to a string;
$var1 = 0;
$var1 = (string) $var1;
if($var1=="some_string")
{
echo "this returns false";
}
----
Server IP: 209.41.74.194
Probable Submitter: 67.177.5.164
----
Manual Page -- http://www.php.net/manual/en/language.types.string.php
Edit -- https://master.php.net/note/edit/88950
Del: integrated -- https://master.php.net/note/delete/88950/integrated
Del: useless -- https://master.php.net/note/delete/88950/useless
Del: bad code -- https://master.php.net/note/delete/88950/bad+code
Del: spam -- https://master.php.net/note/delete/88950/spam
Del: non-english -- https://master.php.net/note/delete/88950/non-english
Del: in docs -- https://master.php.net/note/delete/88950/in+docs
Del: other reasons-- https://master.php.net/note/delete/88950
Reject -- https://master.php.net/note/reject/88950
Search -- https://master.php.net/manage/user-notes.php