Re: [PHP] Re: Weird comparison error.

2003-06-30 Thread Rob Adams

Lars Torben Wilson [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 On Sat, 2003-06-28 at 11:28, Bobby Patel wrote:
  In PHP there are two operators for comparisons, the double and triple
  equivalance.
 
  Double equivalance just check's the boolean type so if (0 == 'n') is
  translated to if (False == False), where as triple equivalance checks
the
  data types as well so if (0==='n') becomes if
(Int(False)==String(False))
  but since the data types don't match that would be False.
 
  to get a better reference check PHP.net for 'Bolean Types' , because  I
  think I dodn't explain the double equivalance right, and also it will
  clarify situations of the Null type.
 
  Bobby

 You got pretty close, but not quite. :) In the case of the equality
 operator '==', there is nothing specific to do with boolean evaluation
 going on. It just converts the string to its integer value implicitly
 before checking for equality, and according to the string-integer
 conversion rules, the string ends up being (int) 0 so the whole thing
 matches.

 Also, it might be easier to think about the identical operator '===' as
 just testing whether two values are identical, meaning that they are
 both of the same type, and that they both have the same value.


Yeah, I understand the difference between the double and triple '='.  I was
just surprised to find that when comparing what was two string values,
because one of the strings was '0', it decided to convert both strings to an
integer.  I'll just have to use the '===' or strcmp from now on.


 More information:

 http://www.php.net/manual/en/language.types.type-juggling.php

http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

 ...in fact, the whole 'Language' section of the manual has a bunch of
 stuff on this.


 Hope this helps,

Thanks.


 Torben


  -- Rob




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



[PHP] Re: Weird comparison error.

2003-06-28 Thread Bobby Patel
In PHP there are two operators for comparisons, the double and triple
equivalance.

Double equivalance just check's the boolean type so if (0 == 'n') is
translated to if (False == False), where as triple equivalance checks the
data types as well so if (0==='n') becomes if (Int(False)==String(False))
but since the data types don't match that would be False.

to get a better reference check PHP.net for 'Bolean Types' , because  I
think I dodn't explain the double equivalance right, and also it will
clarify situations of the Null type.

Bobby


Rob Adams [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 I don't need anything fixed, I'm just curious about something I noticed.

 I'm doing a comparison between a variable and a hard coded char.  Like
this:
 if ($k1 == n)

 The variable is usually the first key in an array (0) so it should usually
 evaluate false, but it was true every time until I changed it to:
 if ($k1 === n)

 So I'm assuming that it decided that since $k1 was an int to convert n
to
 an int (which would be 0) and conclude that 0 does indeed equal n.  So I
 decided for fun to try:
 if (n == $k1)

 And it still was true everytime.  So why does it always try convert my
 literal to an int instead of use the variable as a string?

 Just curious.  Thanks.

   -- Rob





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



Re: [PHP] Re: Weird comparison error.

2003-06-28 Thread Lars Torben Wilson
On Sat, 2003-06-28 at 11:28, Bobby Patel wrote:
 In PHP there are two operators for comparisons, the double and triple
 equivalance.
 
 Double equivalance just check's the boolean type so if (0 == 'n') is
 translated to if (False == False), where as triple equivalance checks the
 data types as well so if (0==='n') becomes if (Int(False)==String(False))
 but since the data types don't match that would be False.
 
 to get a better reference check PHP.net for 'Bolean Types' , because  I
 think I dodn't explain the double equivalance right, and also it will
 clarify situations of the Null type.
 
 Bobby

You got pretty close, but not quite. :) In the case of the equality
operator '==', there is nothing specific to do with boolean evaluation 
going on. It just converts the string to its integer value implicitly
before checking for equality, and according to the string-integer
conversion rules, the string ends up being (int) 0 so the whole thing
matches.

Also, it might be easier to think about the identical operator '===' as
just testing whether two values are identical, meaning that they are
both of the same type, and that they both have the same value.

More information:

http://www.php.net/manual/en/language.types.type-juggling.php
http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

...in fact, the whole 'Language' section of the manual has a bunch of
stuff on this.


Hope this helps,

Torben

-- 
 Torben Wilson [EMAIL PROTECTED]+1.604.709.0506
 http://www.thebuttlesschaps.com  http://www.inflatableeye.com
 http://www.hybrid17.com  http://www.themainonmain.com
 - Boycott Starbucks!  http://www.haidabuckscafe.com -




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