问题
Is $a == $b
always equivalent to $b == $a
?
I think in JavaScript there are a few weird cases where that's not true, due to casting.
I think ide is correct. I'll ask another question.
回答1:
Depends what happens between those two calls. Otherwise yes, those are the same. The order makes no difference. Using 2 equals ==
A string of 1 and integer of 1, will return true when compared. Type is ignored, only value is compared. So no wierdness.
http://php.net/manual/en/language.operators.comparison.php
<?
$a=(string) 1;
$b=(int) 1;
var_dump($a);
var_dump($b);
echo $a==$b;
Outputs: 1
http://www.ideone.com/JLJWQ
EDIT
To clarify, there is absolutely nothing you can ever put in $a or $b to get a different output on the comparison, just by putting it on the other side of the operator.
$a="1234";
$b="1234";
echo $a==$b;
echo $b==$a;
The output of that, for any $a or $b values, will always without a doubt be true true, or false false.
回答2:
In short, yes. $a == $b
will always be the equivalent of $b == $a
. There are some short comings, such as floats. Of course, you shouldn't be nesting two float for equality anyways.
EDIT
Concerning floats: If you had two float and compared them, they technically should be the same. However, floating point values which seem to have the same value do not need to actually be identical. So, if $a
is a literal .69
and $b
is the result of a calculation, they can very well be different, but both display the same value. This is why you should never compare floating-point values by using the ==
.
If you need to compare floating-point values, you really need to use the smallest acceptable difference in your specific case. Something like this would work for comparing floats (setting our smallest acceptable difference at 0.000001
):
if(abs($a-$b) < 0.000001) {
//Same
}
PHP: abs - Absolute Value
回答3:
The only type that I could see being different is something like:
$foo = 1;
$bar = 1;
($foo = $foo + $bar) == ($bar = $foo);
To see why, look at it
A -> ($foo = $foo + $bar)
B -> ($bar = $foo);
If A
is run first, the result will be 2
and the result of B
will be 2, so they are equal and the test will be true
.
If B
is run first, the result will be 1
, and the result of B
will be 2, so they are not equal and the test will be false
.
But for any single type comparison (Where A
is a variable and not an expression) it will always be reflexive.
So in the general sense, A == B
is not always 100% guaranteed to be equivalent to B == A
. For variables, it will always be equivalent. But for complex expressions involving assignment or modification of variables it may not be.
回答4:
http://php.net/manual/en/language.operators.comparison.php
There are diffrent operators you can use if you want to consider type casting in the comparison. ==
evaluates as true on equal value, but does not compare data type. ===
evaluates as true when values are equal as well as datatypes. Using the latter considers type casting where it would normally be ignored (eg: string that represents an integer and an integer being compared.)
The order of the logic in the conditional should not make a difference.
回答5:
I have tried a number of variations and cannot find a case where ($a == $b) !== ($b == $a)
but none so far have worked:
<?php
$a = 0;
$b = "0";
echo (($a == $b) == ($b == $a)) ? "OK\n" : "FAIL\n";
$a = 0;
$b = NULL;
echo (($a == $b) == ($b == $a)) ? "OK\n" : "FAIL\n";
$a = 0;
$b = false;
echo (($a == $b) == ($b == $a)) ? "OK\n" : "FAIL\n";
$a = false;
$b = NULL;
echo (($a == $b) == ($b == $a)) ? "OK\n" : "FAIL\n";
$a = "";
$b = NULL;
echo (($a == $b) == ($b == $a)) ? "OK\n" : "FAIL\n";
$a = "NULL";
$b = NULL;
echo (($a == $b) == ($b == $a)) ? "OK\n" : "FAIL\n";
$a = 0.000000000000000000000000001;
$b = 0;
echo (($a == $b) == ($b == $a)) ? "OK\n" : "FAIL\n";
$a = array();
$b = array();
echo (($a == $b) == ($b == $a)) ? "OK\n" : "FAIL\n";
So, I give up at this point. Ideas welcome!
来源:https://stackoverflow.com/questions/4752579/are-all-php-equality-comparisons-symmetric