PHP 中 !== 和 === 是什么意思?

发布于 2024-10-01 00:14:49 字数 626 浏览 8 评论 0原文

可能的重复:
如何相等(== 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 技术交流群。

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

发布评论

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

评论(6

一页 2024-10-08 00:14:49

它们是identity等价运算符。

1 == 1
1 == "1"
1 === 1
1 !== "1"
true === true
true !== "true"
true == "true"

所有这些都等于真实。
另请检查 @mbeckish 提供的此链接

They are identity equivalence operators.

1 == 1
1 == "1"
1 === 1
1 !== "1"
true === true
true !== "true"
true == "true"

All of these equate to true.
Also check this link provided by @mbeckish

还不是爱你 2024-10-08 00:14:49

它们是严格的类型比较运算符。他们不仅检查,还检查类型

考虑比较数字或字符串时的情况:

if (4 === 4) // same value and type
{
  // true
}

适用

if (4 == "4") // same value and different type but == used
{
  // true
}

但这

if (4 === "4") // same value but different type
{
  // false
}

于对象和数组。

因此,在上述情况下,您必须做出明智的选择,是使用 == 还是 ===

使用 === 是个好主意当您也确定类型时

更多信息:

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:

if (4 === 4) // same value and type
{
  // true
}

but

if (4 == "4") // same value and different type but == used
{
  // true
}

and

if (4 === "4") // same value but different type
{
  // false
}

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 well

More Info:

一个人练习一个人 2024-10-08 00:14:49

=== 还检查变量的类型。

例如,"1" == 1 返回 true,但 "1" === 1 返回 false。它对于可能返回 0 或 False 的函数(例如 strpos)特别有用。

这无法正常工作,因为 strpos 返回 0 且 0 == false

if (strpos('hello', 'hello world!'))

但是,这可以工作:

if (strpos('hello', 'hello world!') !== 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

if (strpos('hello', 'hello world!'))

This, however, would work :

if (strpos('hello', 'hello world!') !== false)
奢欲 2024-10-08 00:14:49

双 = 符号是一种比较,测试左侧的变量/表达式/常量是否与右侧的变量/表达式/常量具有相同的值。

三重 = 符号是一种比较,用于查看两个变量/表达式/常量是否相等并且具有相同的类型 - 即都是字符串或都是整数。

同样的概念适用于 !==

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 !==

幻梦 2024-10-08 00:14:49

仅当给定值的类型和值相等时,它们才会返回 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

温柔少女心 2024-10-08 00:14:49

当您使用两个等号 == 时,它将检查相同的值。

if( '1' == 1 ) { echo 'yes'; }

上面的代码之所以有效,是因为它们具有相同的值。

但如果您使用三个等号===,它将检查值和数据类型。

因此

if( '1' === 1 ) { /* this will not work */ }

这是因为 '1' 的数据类型为 string,而 1integer>number

但你可以这样做 - 我认为:D

if( (integer) '1' === 1 ) { echo 'this works'; }

因为我们正在将 '1' 的数据类型更改为 integer

When you use two equal signs == it will check for the same value.

if( '1' == 1 ) { echo 'yes'; }

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

if( '1' === 1 ) { /* this will not work */ }

This is because '1' has a data type of string while 1 is an integer or a number

But you could do something like this - I think :D

if( (integer) '1' === 1 ) { echo 'this works'; }

Because we are changing the data type of '1' to an integer

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