Re: Odd Code Error.
| From: | Jonathan Pitcher | Date: | Wed, 21 Jan 2004 20:38:43 +0000 |
| Subject: | Re: Odd Code Error. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-175335@lists.php.net to get a copy of this message | ||
Thanks. It makes sense now.
Now I have one more question. Not to confuse the issue more. :)
$EA = 0
if ($EA == "NFH") // would work because the string is converted to an integer and then compared correct ?
And by using === I tell it to compare type and value ? If that is true would 0 == "0"? I know I can run it through a string to integer function which is probably a good idea anyways.
Thanks again for all your responses.
Jonathan Pitcher
On Jan 21, 2004, at 9:21 AM, Ford, Mike [LSS] wrote:
On 21 January 2004 04:01, Luke contributed these pearls of wisdom:ok, i read the section, but even so if $a == $b and $a == $c then $b should be equal to $cNo, not necessarily!but php is saying otherwise?Yes.this sounds confusing i want to try n get my head round it a string equals a integer of zero, and a string equals true, but the reason the bool doesnt equal the int is because when the string and int are compared, the string is zero (because it has no numerical value)? did that make sense? am i right?Well, I'm not sure 'cos I find your reasoning hard to follow...!! ;) The essentials go like this: when performing most comparisons (==, !=, <, >, <=, >=), if the operands are of different types then at least one of them has a type conversion performed on it -- and it's the rules about which one is converted that determines what the outcome will be. The following is based on empirical results, but I think is fairly accurate (except, possibly, in one or two edge cases): * If either operand is numeric (integer or float), the other one is converted to the same type and the comparison performed. * If either operand is Boolean, the other operand is converted to Boolean and the comparison performed. * If both operands are strings: - if both are strictly representations of decimalnumbers, convert both to numeric and compare.- otherwise compare as strings. * Otherwise (I think, but who really cares by now!) do exact comparison. Note particularly the behaviour if both operands are strings -- this means that, for example: '0123' == '123' and '0123' > '122.3' but '0123a' != '123a' and '0123x' < '122.3x' Thus, you shouldn't use loose comparisons for comparing strings if there's *any* chance that both operands will look like numbers *and* you want a strictly string-type comparison -- in those circumstances, strcmp() is very much your friend. ;) Cheers! Mike -- Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning & Information Services, JG125, James Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6 3QS, United Kingdom Email: m.ford@leedsmet.ac.ukTel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php