所有 PHP 相等比较都是对称的吗?

发布于 2024-10-13 02:32:39 字数 349 浏览 3 评论 0原文

$a == $b 总是等价于 $b == $a 吗?

我认为在 JavaScript 中,由于强制转换,有一些奇怪的情况并非如此。

我认为 ide 是正确的。我会问另一个问题

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抠脚大汉 2024-10-20 02:32:39

简而言之,是的。 $a == $b 始终等价于 $b == $a。有一些缺点,例如浮动。当然,无论如何,您都不应该嵌套两个浮点数以实现相等。

编辑
关于浮动:如果您有两个浮动并比较它们,那么它们在技术上应该是相同的。然而,看似具有相同值的浮点值实际上并不需要相同。因此,如果 $a 是文字 .69$b 是计算结果,它们很可能不同,但两者显示相同的值。这就是为什么您永远不应该使用 == 来比较浮点值。

如果您需要比较浮点值,您确实需要在特定情况下使用最小的可接受差异。像这样的东西可以用于比较浮点数(将我们可接受的最小差异设置为0.000001):

if(abs($a-$b) < 0.000001) {
  //Same
}

PHP:abs - 绝对值

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

对岸观火 2024-10-20 02:32:39

我能看到的唯一不同的类型是这样的:

$foo = 1;
$bar = 1;

($foo = $foo + $bar) == ($bar = $foo);

要了解原因,请查看它

A -> ($foo = $foo + $bar)
B -> ($bar = $foo);

如果首先运行 A,结果将是 2A 的结果code>B 将是 2,因此它们相等并且测试将为 true

如果先运行B,则结果将为1,而B的结果将为2,因此它们不相等,测试将是

但对于任何单一类型比较(其中 A 是变量而不是表达式),它始终是自反的。

因此,从一般意义上来说,A == B 并不总是 100% 保证等于 B == A。对于变量来说,它总是等价的。但对于涉及变量赋值或修改的复杂表达式,情况可能并非如此。

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.

又怨 2024-10-20 02:32:39

取决于这两个调用之间发生的情况。否则是的,这些都是一样的。顺序没有区别。使用 2 equals == 1 的字符串和 1 的整数,比较时将返回 true。类型被忽略,仅比较值。所以没有什么奇怪的。

http://php.net/manual/en/language.operators.comparison.php

<?

$a=(string) 1;
$b=(int) 1;

var_dump($a);
var_dump($b);


echo $a==$b;

输出:1

http://www.ideone.com/JLJWQ

编辑< /b>

需要澄清的是,您绝对无法在 $a 或 $b 中放入任何内容来获得不同的比较输出,只需将其放在运算符的另一侧即可。

$a="1234";
$b="1234";

echo $a==$b;
echo $b==$a;

对于任何 $a 或 $b 值,其输出毫无疑问总是 true true 或 false false。

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.

万劫不复 2024-10-20 02:32:39

http://php.net/manual/en/language.operators.comparison.php

如果您想在比较中考虑类型转换,可以使用不同的运算符。 == 在相等的值上求值为 true,但不比较数据类型。当值和数据类型相等时,=== 计算结果为 true。使用后者会考虑通常被忽略的类型转换(例如:表示整数的字符串和正在比较的整数。)

条件中的逻辑顺序不应产生差异。

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.

我不在是我 2024-10-20 02:32:39

我尝试了多种变体,但找不到 ($a == $b) !== ($b == $a) 的情况,但到目前为止没有一个起作用:

<?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";

所以,我给出此时。欢迎提出想法!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文