PHP 中 !== 和 === 是什么意思?
可能的重复:
如何相等(== double equals)和恒等(===三等号)比较运算符不同吗?
参考 - 这个符号在 PHP 中意味着什么?
php 不等于 != 和 !==
什么是 != =
和 ===
运算符在此代码片段中?
if ( $a !== null ) // do something
if ( $b === $a ) // do something
Possible Duplicates:
How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?
Reference - What does this symbol mean in PHP?
php not equal to != and !==
What are the !==
and ===
operators in this code snippet?
if ( $a !== null ) // do something
if ( $b === $a ) // do something
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它们是
identity等价运算符。所有这些都等于真实。
另请检查 @mbeckish 提供的此链接
They are
identityequivalence operators.All of these equate to true.
Also check this link provided by @mbeckish
它们是严格的类型比较运算符。他们不仅检查值,还检查类型。
考虑比较数字或字符串时的情况:
适用
但这
于对象和数组。
因此,在上述情况下,您必须做出明智的选择,是使用
==
还是===
使用
===
是个好主意当您也确定类型时更多信息:
They are strict type comparison operators. They not only check the value but also the type.
Consider a situation when you compare numbers or strings:
but
and
This applies to objects as well as arrays.
So in above cases, you have to make sensible choice whether to use
==
or===
It is good idea to use
===
when you are sure about the type as wellMore Info:
=== 还检查变量的类型。
例如,
"1" == 1
返回 true,但"1" === 1
返回 false。它对于可能返回 0 或 False 的函数(例如 strpos)特别有用。这无法正常工作,因为 strpos 返回 0 且 0 == false
但是,这可以工作:
=== also checks for the type of the variable.
For instance,
"1" == 1
returns true but"1" === 1
returns false. It's particularly useful for fonctions that may return 0 or False (strpos for instance).This wouldn't work correctly because strpos returns 0 and 0 == false
This, however, would work :
双 = 符号是一种比较,测试左侧的变量/表达式/常量是否与右侧的变量/表达式/常量具有相同的值。
三重 = 符号是一种比较,用于查看两个变量/表达式/常量是否相等并且具有相同的类型 - 即都是字符串或都是整数。
同样的概念适用于 !==
A double = sign is a comparison and tests whether the variable / expression / constant to the left has the same value as the variable / expression / constant to the right.
A triple = sign is a comparison to see whether two variables / expresions / constants are equal AND have the same type - i.e. both are strings or both are integers.
The same concept applies for !==
仅当给定值的类型和值相等时,它们才会返回 true。
例子:
1 === 1 为真
"1" === 1 为假
1 === “1”为假
"1" === "1" 为真
,与 == 一样,上述所有内容均为真
They will only return true if both the type and value of the values given are equivalent.
Example:
1 === 1 is true
"1" === 1 is false
1 === "1" is false
"1" === "1" is true
where as with == all of the above would be true
当您使用两个等号
==
时,它将检查相同的值。上面的代码之所以有效,是因为它们具有相同的值。
但如果您使用三个等号
===
,它将检查值和数据类型。因此
这是因为
'1'
的数据类型为string
,而1
是integer
或>number
但你可以这样做 - 我认为:D
因为我们正在将
'1'
的数据类型更改为integer
When you use two equal signs
==
it will check for the same value.The above code works because they have the same value.
But if you use three equal signs
===
it will check the value and the data type.Therefore
This is because
'1'
has a data type ofstring
while1
is aninteger
or anumber
But you could do something like this - I think :D
Because we are changing the data type of
'1'
to aninteger