weird comparsion

weird comparsion

am 03.05.2007 13:08:12 von Oskar

Hello,

Why does anything compared to 0 return true?

I know it might seem to be a bit off-topic, but for me it is important
for detecting if NULL value in table has been changed (for example NULL
is changed to 0).

if ("foo"==0) echo("foo equals to 0");

OKi98

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: weird comparsion

am 03.05.2007 13:17:05 von Mike van Hoof

http://nl3.php.net/manual/en/language.operators.comparison.p hp

try using === (3x =) for comparison

- Mike

OKi98 schreef:
> Hello,
>
> Why does anything compared to 0 return true?
>
> I know it might seem to be a bit off-topic, but for me it is important
> for detecting if NULL value in table has been changed (for example
> NULL is changed to 0).
>
> if ("foo"==0) echo("foo equals to 0");
>
> OKi98
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: weird comparsion

am 03.05.2007 13:21:43 von Aleksandar Vojnovic

try comparing the type:

if("0" === 0)echo 'Its not equal';
if(0 === 0)echo 'Its equal';



OKi98 wrote:
> Hello,
>
> Why does anything compared to 0 return true?
>
> I know it might seem to be a bit off-topic, but for me it is important
> for detecting if NULL value in table has been changed (for example
> NULL is changed to 0).
>
> if ("foo"==0) echo("foo equals to 0");
>
> OKi98
>

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: weird comparsion

am 03.05.2007 13:30:22 von Oskar

Mike van Hoof wrote:

> http://nl3.php.net/manual/en/language.operators.comparison.p hp
>
> try using === (3x =) for comparison
>
> - Mike
>
> OKi98 schreef:
>
>> Hello,
>>
>> Why does anything compared to 0 return true?
>>
>> I know it might seem to be a bit off-topic, but for me it is
>> important for detecting if NULL value in table has been changed (for
>> example NULL is changed to 0).
>>
>> if ("foo"==0) echo("foo equals to 0");
>>
>> OKi98
>>
>
>
> __________ Informace od NOD32 2235 (20070502) __________
>
> Tato zprava byla proverena antivirovym systemem NOD32.
> http://www.nod32.cz
>
>
>
I know about identity operator (===) but with == operator 0 is false and
"foo" is true, try this:

$foo=0;
$bar="bar";
if ($foo) echo("$foo is true, ");
else echo("$foo is false, ");
if ($bar) echo("$bar is true, ");
else echo("$bar is false, ");
if ($foo==$bar) echo("$foo==$bar");

returns "0 is false, bar is true, 0==$bar"

OKi98

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: weird comparsion

am 03.05.2007 14:42:31 von M.Ford

T24gMDMgTWF5IDIwMDcgMTI6MzAsIE9LaTk4IHdyb3RlOg0KDQo+IEkga25v
dyBhYm91dCBpZGVudGl0eSBvcGVyYXRvciAoPT09KSBidXQgd2l0aCA9PSBv
cGVyYXRvciAwIGlzIGZhbHNlDQo+IGFuZCAiZm9vIiBpcyB0cnVlDQoNCk5v
LCB0aGF0J3Mgbm90IGNvcnJlY3QuDQoNCiAgLCB0cnkgdGhpczoNCj4gDQo+
ICRmb289MDsNCj4gJGJhcj0iYmFyIjsNCj4gaWYgKCRmb28pIGVjaG8oIiRm
b28gaXMgdHJ1ZSwgIik7DQo+ICAgICBlbHNlIGVjaG8oIiRmb28gaXMgZmFs
c2UsICIpOw0KPiBpZiAoJGJhcikgZWNobygiJGJhciBpcyB0cnVlLCAiKTsN
Cj4gICAgIGVsc2UgZWNobygiJGJhciBpcyBmYWxzZSwgIik7DQo+IGlmICgk
Zm9vPT0kYmFyKSBlY2hvKCIkZm9vPT0kYmFyIik7DQo+IA0KPiByZXR1cm5z
ICIwIGlzIGZhbHNlLCBiYXIgaXMgdHJ1ZSwgMD09JGJhciINCg0KVGhhdCdz
IGJlY2F1c2UgeW91J3ZlIGdvdCBsb2FkcyBvZiBpbXBsaWNpdCB0eXBlIGNv
bnZlcnNpb25zIGdvaW5nIG9uIHRoZXJlLCBzbyB5b3UncmUgbm90IGNvbXBh
cmluZyBsaWtlIHdpdGggbGlrZS4NCg0KRm9yICBpZiAoJGZvbykgLi4uIGFu
ZCBpZiAoJGJhcikgLi4uOg0KDQp3aXRoaW4gdGhlIG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VuIHN1cnByaXNlcyBsaWtlIHRoaXMuDQoNCkNoZWVycyENCg0K
TWlrZQ0KDQotLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0NCk1pa2UgRm9yZCwg
IEVsZWN0cm9uaWMgSW5mb3JtYXRpb24gU2VydmljZXMgQWR2aXNlciwNCkpH
MTI1LCBUaGUgSGVhZGluZ2xleSBMaWJyYXJ5LA0KSmFtZXMgR3JhaGFtIEJ1
aWxkaW5nLCBMZWVkcyBNZXRyb3BvbGl0YW4gVW5pdmVyc2l0eSwNCkhlYWRp
bmdsZXkgQ2FtcHVzLCBMRUVEUywgIExTNiAzUVMsICBVbml0ZWQgS2luZ2Rv
bQ0KRW1haWw6IG0uZm9yZEBsZWVkc21ldC5hYy51aw0KVGVsOiArNDQgMTEz
IDgxMiA0NzMwICAgICAgICAgIEZheDogICs0NCAxMTMgODEyIDMyMTEgDQoK
ClRvIHZpZXcgdGhlIHRlcm1zIHVuZGVyIHdoaWNoIHRoaXMgZW1haWwgaXMg
ZGlzdHJpYnV0ZWQsIHBsZWFzZSBnbyB0byBodHRwOi8vZGlzY2xhaW1lci5s
ZWVkc21ldC5hYy51ay9lbWFpbC5odG0K

Re: weird comparsion

am 03.05.2007 17:15:00 von Oskar

Ford, Mike wrote:

>On 03 May 2007 12:30, OKi98 wrote:
>
>
>
>>I know about identity operator (===) but with == operator 0 is false
>>and "foo" is true
>>
>>
>
>No, that's not correct.
>
> , try this:
>
>
>>$foo=0;
>>$bar="bar";
>>if ($foo) echo("$foo is true, ");
>> else echo("$foo is false, ");
>>if ($bar) echo("$bar is true, ");
>> else echo("$bar is false, ");
>>if ($foo==$bar) echo("$foo==$bar");
>>
>>returns "0 is false, bar is true, 0==$bar"
>>
>>
>
>That's because you've got loads of implicit type conversions going on there, so you're not comparing like with like.
>
>For if ($foo) ... and if ($bar) ...:
>
>within the context of the if(), both $foo and $bar are implicitly converted to Boolean:
> - (bool)0 is FALSE
> - (bool)"any non-empty() string" is TRUE
>
>
>On the other hand, for if ($foo==$bar) ...:
>
>in the context of the == comparison, $bar is converted to a number, and any string not beginning with a numeric character converts to numeric zero -- so you get a comparison of zero with zero, which is, of course, TRUE!
>
>
oh I didnt know that, I thought the number will be converted into a
string. Thanks alot.

I have one more question but I ask in separate thread :)

Oki98

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: weird comparsion

am 04.05.2007 13:25:11 von itoctopus

You have to use the type equality
=== (3 equals), otherwise, what the comparision does it that it will compare
the value of "foo" as a number (which is 0) to 0.
Note that there are php functions, such as strpos, who can lead to serious
flaws in your program if you use the type equality.
strpos returns 0 if the position of the string is 0 in the other string,
however, it returns FALSE if it is not.

Hope that helps,

--
itoctopus - http://www.itoctopus.com
"OKi98" wrote in message
news:4639C29C.50202@centrum.cz...
> Hello,
>
> Why does anything compared to 0 return true?
>
> I know it might seem to be a bit off-topic, but for me it is important for
> detecting if NULL value in table has been changed (for example NULL is
> changed to 0).
>
> if ("foo"==0) echo("foo equals to 0");
>
> OKi98

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php