PHP平等(==双重平等)和身份(===三重等于)的比较操作员有何不同?
==
和===
之间有什么区别?
- 松散的
==
比较如何工作? - 严格的
===
比较如何工作?
有什么有用的例子?
What is the difference between ==
and ===
?
- How exactly does the loosely
==
comparison work? - How exactly does the strict
===
comparison work?
What would be some useful examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
==
和===
之间的区别松散
==
等于运算符和严格===
之间的区别> 相同的运算符在手册中得到了准确的解释:比较运算符
松散
==
相等比较如果您使用
==
运算符,或使用松散比较的任何其他比较运算符,例如!=
,< code><> 或==
,您始终必须查看上下文以查看转换的内容、位置和原因,以了解转换的内容正在进行中。转换规则
类型比较表
作为参考和示例,您可以看到手册:
严格
===
相同比较如果您使用的是
===
运算符,或任何其他使用严格比较的比较运算符,例如!==
或===
,那么您始终可以确保类型不会神奇地改变,因为不会进行任何转换。因此,通过严格比较,类型和值必须相同,而不仅仅是值。类型比较表
作为参考和示例,您可以在 中查看比较表手册:
与
===
严格比较Difference between
==
and===
The difference between the loosely
==
equal operator and the strict===
identical operator is exactly explained in the manual:Comparison Operators
Loosely
==
equal comparisonIf you are using the
==
operator, or any other comparison operator which uses loosely comparison such as!=
,<>
or==
, you always have to look at the context to see what, where and why something gets converted to understand what is going on.Converting rules
Type comparison table
As reference and example you can see the comparison table in the manual:
Strict
===
identical comparisonIf you are using the
===
operator, or any other comparison operator which uses strict comparison such as!==
or===
, then you can always be sure that the types won't magically change, because there will be no converting going on. So with strict comparison the type and value have to be the same, not only the value.Type comparison table
As reference and example you can see the comparison table in the manual:
Strict comparisons with
===
如果两个不同类型不同,则运算符 == 在它们之间进行强制转换,而 === 运算符则执行“类型安全比较”。这意味着只有当两个操作数具有相同的类型和相同的值时才会返回 true。
示例:
警告:具有等效成员的同一类的两个实例与
===
运算符不匹配。例子:The operator == casts between two different types if they are different, while the === operator performs a 'typesafe comparison'. That means that it will only return true if both operands have the same type and the same value.
Examples:
Warning: two instances of the same class with equivalent members do NOT match the
===
operator. Example:一图胜千字:
PHP Double Equals
==
等式图表:PHP 三重等于
===
等式图表:创建这些图像的源代码:PHP 平等图表
大师冥想
那些希望保持理智的人,请不要继续阅读,因为这些都没有任何意义,除非说这就是 PHP 的疯狂分形的设计方式。
NAN != NAN
但NAN == true
。==
将把左右操作数转换为数字。所以123 == "123foo"
,但是"123" != "123foo"
引号中的十六进制字符串有时是浮点数,并且会意外地转换为违背你的意愿浮动,导致运行时错误。
==
不具有传递性,因为"0"== 0
,并且0 == ""
但"0" != ""
尚未声明的 PHP 变量 为 false。尽管 PHP 有一种方法来表示未定义的变量,但该功能通过
==
被禁用。“6”==“6”
、“4.2”==“4.20”
和“133”==“0133”
>,但是133!= 0133
。但是"0x10" == "16"
和"1e3" == "1000"
会在未经您的指示或同意的情况下发生意外的字符串转换为八进制,从而导致运行时错误。False == 0
、""
、[]
和"0"
。如果您将数字加 1 并且它们已经保持最大值,则它们不会回绕。相反,它们被转换为
无穷大
。新班级 == 到 1。
False 是最危险的值,因为 False 是 == 到大多数其他变量,这大多违背了它的目的。
希望:
如果您使用 PHP,则不应使用双等号运算符,因为如果您使用三等号,则唯一需要担心的边缘情况是 NAN 和数字非常接近其数据类型的最大值,以至于它们会被强制转换为无穷大。使用双等号,任何东西都可以是令人惊讶的
==
到任何东西,或者可以是违背你的意愿的惊喜,并且!=
到它显然应该相等的东西。在 PHP 中使用
==
的任何地方都是一种糟糕的代码味道,因为隐式转换规则暴露了 85 个错误,这些规则似乎是由数百万程序员通过布朗运动编程设计的。A picture is worth a thousand words:
PHP Double Equals
==
equality chart:PHP Triple Equals
===
Equality chart:Source code to create these images: PHP equality charts
Guru Meditation
Those who wish to keep their sanity, read no further because none of this will make any sense, except to say that this is how the insanity-fractal, of PHP was designed.
NAN != NAN
butNAN == true
.==
will convert left and right operands to numbers if left is a number. So123 == "123foo"
, but"123" != "123foo"
A hex string in quotes is occasionally a float, and will be surprise cast to float against your will, causing a runtime error.
==
is not transitive because"0"== 0
, and0 == ""
but"0" != ""
PHP variables that have not been declared yet are false. Even though PHP has a way to represent undefined variables, that feature is disabled with
==
."6" == " 6"
,"4.2" == "4.20"
, and"133" == "0133"
, but133 != 0133
. But"0x10" == "16"
and"1e3" == "1000"
exposing that surprise string conversion to octal will occur both without your instruction or consent, causing a runtime error.False == 0
,""
,[]
and"0"
.If you add 1 to number and they are already holding their maximum value, they do not wrap around. Instead, they are cast to
infinity
.A fresh class is == to 1.
False is the most dangerous value because False is == to most of the other variables, mostly defeating its purpose.
Hope:
If you are using PHP, Thou shalt not use the double equals operator because if you use triple equals, the only edge cases to worry about are NAN and numbers so close to their datatype's maximum value, that they are cast to infinity. With double equals, anything can be surprise
==
to anything or, or can be surprise cast against your will and!=
to something of which it should obviously be equal.Anywhere you use
==
in PHP is a bad code smell because of the 85 bugs in it exposed by implicit casting rules that seem designed by millions of programmers programming by Brownian motion.关于JavaScript:
===操作员的工作与==运算符相同,但它要求其操作数不仅具有相同的值,而且具有相同的数据类型。
例如,下面的示例将显示“ x和y相等”,而不是“ x和y是相同的”。
In regards to JavaScript:
The === operator works the same as the == operator, but it requires that its operands have not only the same value, but also the same data type.
For example, the sample below will display 'x and y are equal', but not 'x and y are identical'.
有关对象比较的其他答案的补充:
==使用对象的名称及其值比较对象。如果两个对象具有相同的类型并且具有相同的成员值,则
$ a == $ b
产生true。===比较对象的内部对象ID。即使成员相等,
$ a!== $ b
如果它们不是完全相同的对象。An addition to the other answers concerning object comparison:
== compares objects using the name of the object and their values. If two objects are of the same type and have the same member values,
$a == $b
yields true.=== compares the internal object id of the objects. Even if the members are equal,
$a !== $b
if they are not exactly the same object.这都是关于数据类型的。以
BOOL
(true 或 false)为例:true
也等于1
并且false
也等于0
比较时
==
不关心数据类型:因此,如果您有一个为 1 的变量(也可能是
true
):$var=1;
然后与
==
进行比较:但是
$var
实际上并不等于true
,不是吗?它的 int 值为1
,而该值又等于 true。使用
===
,检查数据类型以确保两个变量/对象/任何内容使用相同的类型。因此,如果我这样做,
这个条件就不会成立,因为
$var !== true
它只是== true
(如果你知道我的意思)。你为什么需要这个?
简单 - 让我们看一下 PHP 的一个函数:
array_search()
:array_search()
函数只是在数组中搜索某个值,然后返回该值的键找到该值的元素。如果在数组中找不到该值,则返回false。但是,如果您对存储在数组的第一个元素中的值执行array_search()
(数组键为0< /code>)....
array_search()
函数将返回 0...这等于 false..因此,如果您这样做:
那么,您知道这现在怎么会成为一个问题吗? ?
大多数人在检查函数是否返回 false 时不会使用
== false
。相反,他们使用!
。但实际上,这与使用==false
完全相同,因此如果您这样做:因此对于类似的事情,您将使用
===
代替,这样检查数据类型。It's all about data types. Take a
BOOL
(true or false) for example:true
also equals1
andfalse
also equals0
The
==
does not care about the data types when comparing:So if you had a variable that is 1 (which could also be
true
):$var=1;
And then compare with the
==
:But
$var
does not actually equaltrue
, does it? It has the int value of1
instead, which in turn, is equal to true.With
===
, the data types are checked to make sure the two variables/objects/whatever are using the same type.So if I did
that condition would not be true, as
$var !== true
it only== true
(if you know what I mean).Why would you need this?
Simple - let's take a look at one of PHP's functions:
array_search()
:The
array_search()
function simply searches for a value in an array, and returns the key of the element the value was found in. If the value could not be found in the array, it returns false. But, what if you did anarray_search()
on a value that was stored in the first element of the array (which would have the array key of0
)....thearray_search()
function would return 0...which is equal to false..So if you did:
So, do you see how this could be an issue now?
Most people don't use
== false
when checking if a function returns false. Instead, they use the!
. But actually, this is exactly the same as using==false
, so if you did:So for things like that, you would use the
===
instead, so that the data type is checked.PHP Double Equals
==
:在大多数编程语言中,比较运算符 (==) 一方面检查数据类型,另一方面检查变量的内容是否相等。 PHP 中的标准比较运算符 (==) 的行为有所不同。这会在比较之前尝试将两个变量转换为相同的数据类型,然后才检查这些变量的内容是否相同。获得以下结果:
PHP Triple Equals
===
:该运算符还会检查变量的数据类型,仅当两个变量具有相同的内容和相同的数据类型时才返回 (bool)true。因此,以下内容是正确的:
阅读 PHP 中 == 和 === 有什么区别
PHP Double Equals
==
:In most programming languages, the comparison operator (==) checks, on the one hand, the data type and on the other hand the content of the variable for equality. The standard comparison operator (==) in PHP behaves differently. This tries to convert both variables into the same data type before the comparison and only then checks whether the content of these variables is the same. The following results are obtained:
PHP Triple Equals
===
:This operator also checks the datatype of the variable and returns (bool)true only if both variables have the same content and the same datatype. The following would therefore be correct:
Read more in What is the difference between == and === in PHP
您将使用===测试函数还是变量是错误的,而不仅仅是等于false(零或一个空字符串)。
在这种情况下,STRPO将返回0等于在测试中的false
或
您在这里想要的不是什么。
You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).
In this case strpos would return 0 which would equate to false in the test
or
which is not what you want here.
到目前为止,所有答案都忽略了 === 的危险问题。顺便指出,但没有强调,整数和双精度是不同的类型,因此以下代码:
给出:
请注意,这不是“舍入错误”的情况。这两个数字直到最后一位都完全相等,但它们具有不同的类型。
这是一个令人讨厌的问题,因为如果所有数字都足够小(其中“足够小”取决于您运行的硬件和操作系统),使用 === 的程序可以愉快地运行多年。但是,如果碰巧某个整数恰好足够大,可以转换为双精度型,则其类型将“永远”更改,即使后续操作或许多操作可能会将其值恢复为小整数。而且,情况变得更糟。它可以传播——双重感染可以传播到它接触到的任何东西,一次一个计算。
例如,在现实世界中,这可能是处理 2038 年之后日期的程序中的一个问题。此时,UNIX 时间戳(自 1970-01-01 00:00:00 UTC 以来的秒数)将需要超过 32 位,因此它们的表示形式将在某些系统上“神奇地”切换为双倍。因此,如果您计算两次之间的差异,您可能会得到几秒钟的结果,但作为双精度数,而不是 2017 年发生的整数结果。
我认为这比字符串和数字之间的转换要糟糕得多,因为这是微妙的。我发现跟踪什么是字符串和什么是数字很容易,但是跟踪数字中的位数超出了我的范围。
因此,在上面的答案中,有一些不错的表格,但 1(作为整数)和 1(微妙双精度)和 1.0(明显双精度)之间没有区别。另外,建议您应该始终使用 === 而从不使用 == ,这种建议并不好,因为 === 有时会在 == 正常工作的情况下失败。另外,JavaScript 在这方面并不等效,因为它只有一种数字类型(内部可能有不同的按位表示,但不会导致 === 出现问题)。
我的建议是——两者都不用。您需要编写自己的比较函数才能真正解决这个问题。
All of the answers so far ignore a dangerous problem with ===. It has been noted in passing, but not stressed, that integer and double are different types, so the following code:
gives:
Note that this is NOT a case of a "rounding error". The two numbers are exactly equal down to the last bit, but they have different types.
This is a nasty problem because a program using === can run happily for years if all of the numbers are small enough (where "small enough" depends on the hardware and OS you are running on). However, if by chance, an integer happens to be large enough to be converted to a double, its type is changed "forever" even though a subsequent operation, or many operations, might bring it back to a small integer in value. And, it gets worse. It can spread - double-ness infection can be passed along to anything it touches, one calculation at a time.
In the real world, this is likely to be a problem in programs that handle dates beyond the year 2038, for example. At this time, UNIX timestamps (number of seconds since 1970-01-01 00:00:00 UTC) will require more than 32-bits, so their representation will "magically" switch to double on some systems. Therefore, if you calculate the difference between two times you might end up with a couple of seconds, but as a double, rather than the integer result that occurs in the year 2017.
I think this is much worse than conversions between strings and numbers because it is subtle. I find it easy to keep track of what is a string and what is a number, but keeping track of the number of bits in a number is beyond me.
So, in the above answers there are some nice tables, but no distinction between 1 (as an integer) and 1 (subtle double) and 1.0 (obvious double). Also, advice that you should always use === and never == is not great because === will sometimes fail where == works properly. Also, JavaScript is not equivalent in this regard because it has only one number type (internally it may have different bit-wise representations, but it does not cause problems for ===).
My advice - use neither. You need to write your own comparison function to really fix this mess.
变量有类型和值。
当您使用这些变量(在 PHP 中)时,有时您没有正确的类型。
例如,如果
PHP 必须将 $var 转换(“转换”)为整数。在这种情况下,“$var == 1”为 true,因为任何非空字符串都会转换为 1。
使用 === 时,您会检查值和类型是否相等,因此“$var === 1”是假的。
这很有用,例如,当您有一个可以返回 false(出错时)和 0(结果)的函数时:
此代码是错误的,就像
myFunction()
返回 0 一样,它会被强制转换为 false你似乎有一个错误。正确的代码是:因为测试是返回值“是布尔值并且是假”而不是“可以转换为假”。
Variables have a type and a value.
When you use these variables (in PHP), sometimes you don't have the good type.
For example, if you do
PHP have to convert ("to cast") $var to integer. In this case, "$var == 1" is true because any non-empty string is casted to 1.
When using ===, you check that the value AND THE TYPE are equal, so "$var === 1" is false.
This is useful, for example, when you have a function that can return false (on error) and 0 (result) :
This code is wrong as if
myFunction()
returns 0, it is casted to false and you seem to have an error. The correct code is :because the test is that the return value "is a boolean and is false" and not "can be casted to false".
==(相等)和===(相同相等)之间的区别
PHP 提供了两个比较运算符来检查两个值是否相等。这两者之间的主要区别在于
'=='
检查两个操作数的值是否相等
。另一方面,'==='
检查操作数的值和类型是否相等
。示例 =>
如果我们输入cast $val2 to (int)$val2 or (string)$val1 那么它返回 true
OR
Difference between == (equal) and === (identical equal)
PHP provides two comparison operators to check equality of two values. The main difference between of these two is that
'=='
checks if the values of the two operands areequal or not
. On the other hand,'==='
checks the values as well as the type of operands areequal or not
.Example =>
if we type cast $val2 to (int)$val2 or (string)$val1 then it returns true
OR
PHP 数组和对象中的
==
和===
有两个没有人提到的区别:两个具有不同键排序的数组和对象。具有不同键排序的两个数组
如果您有两个数组,其键排序不同,但具有相同的键值映射,则它们是完全不同的(即使用
===
)。如果您对数组进行键排序,并尝试将排序后的数组与原始数组进行比较,这可能会导致问题。例如:
对象
请记住,主要规则是两个不同的对象永远不会严格相等。请看下面的示例:
注意:将一个对象分配给另一个变量不会创建副本 - 相反,它会创建对同一对象的引用。 请参阅此处。
注意:从 PHP7 开始,匿名类< /a> 被引入。在上面的测试中,
new class {}
和new stdClass()
之间没有区别。There are two differences between
==
and===
in PHP arrays and objects that nobody mentioned: two arrays with different key sorts, and objects.Two arrays with different key sorts
If you have two arrays with their keys sorted differently, but having equal key-value maps, they are strictly different (i.e. using
===
). That might lead to problems, if you key-sort an array, and try to compare the sorted array with the original one.For example:
Objects
Keep in mind, the main rule is that two different objects are never strict-equal. Look at the following example:
Note: Assigning an object to another variable does not create a copy - rather, it creates a reference to the same object. See here.
Note: As of PHP7, anonymous classes was introduced. There is no difference between a
new class {}
and anew stdClass()
in the tests above.