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 $c
No, 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 decimal
numbers, 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: [EMAIL PROTECTED]
Tel: +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