php “===”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1117967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:08:20  来源:igfitidea点击:

What does "===" mean?

phpoperatorscomparison-operatorsidentity-operator

提问by Dykam

I've noticed someone using the PHP operator ===which I can't make sense out of. I've tried it with a function, and it corresponds in crazy ways.

我注意到有人使用了===我无法理解的 PHP 运算符。我用一个函数尝试过它,它以疯狂的方式对应。

What is the definition of this operator? I can't even find it in the declaration of PHP operators.

这个运算符的定义是什么?我什至无法在 PHP 运算符的声明中找到它。

回答by Tim Sylvester

$a === $b     (Identical)      

TRUEif $ais equal to $b, and they are of the same type. (introduced in PHP 4)

$a === $b     (Identical)      

TRUE如果$a等于$b,并且它们是相同的类型。(在 PHP 4 中引入)

PHP Docs

PHP 文档

回答by Dykam

http://www.php.net/ternary

http://www.php.net/ternary

$a == $b Equal TRUE if $a is equal to $b, except for (True == -1) which is still True.

$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.

如果 $a 等于 $b,则 $a == $b 等于 TRUE,除了 (True == -1) 仍然为 True。

$a === $b 相同 如果 $a 等于 $b 则为 TRUE,并且它们的类型相同。

> "5" == 5;
True
> "5" === 5;
False

回答by Artem Barger

You can read here, short summary:

你可以在这里阅读,简短的总结:

$a == $b Equal TRUE if $a is equal to $b after type juggling.

$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.

$a == $b Equal 如果 $a 在类型转换后等于 $b,则为 TRUE。

$a === $b 相同 如果 $a 等于 $b 则为 TRUE,并且它们的类型相同。

回答by farzad

In PHP you may compare two values using the == operator or === operator. The difference is this:

在 PHP 中,您可以使用 == 运算符或 === 运算符比较两个值。区别在于:

PHP is a dynamic, interpreted language that is not strict on data types. It means that the language itself will try to convert data types, whenever needed.

PHP 是一种动态的解释型语言,对数据类型并不严格。这意味着语言本身将在需要时尝试转换数据类型。

echo 4 + "2"; // output is 6

The output is integer value 6, because +is the numerical addition operator in PHP, so if you provide operands with other data types to it, PHP will first convert them to their appropriate type ("2" will be converted to 2) and then perform the operation.

输出是整数值6,因为+是PHP中的数值加法运算符,所以如果你给它提供了其他数据类型的操作数,PHP会先把它们转换成合适的类型(“2”会转换成2)然后执行操作。

If you use == as the comparison operator with two operands that might be in different data types, PHP will convert the second operand type, to the first's. So:

如果您使用 == 作为比较运算符,其中两个操作数可能是不同的数据类型,PHP 会将第二个操作数类型转换为第一个。所以:

4 == "4" // true

4 == "4" // 真

PHP converts "4" to 4, and then compares the values. In this case, the result will be true.

PHP 将“4”转换为 4,然后比较这些值。在这种情况下,结果将为真。

If you use === as the comparison operator, PHP will not try to convert any data types. So if the operands' types are different, then they are NOT identical.

如果您使用 === 作为比较运算符,PHP 将不会尝试转换任何数据类型。因此,如果操作数的类型不同,则它们不相同。

4 === "4" // false

4 === "4" // 假

回答by anik4e

$x == $yis TRUE if the value of the $x and $y are same:

$x == $y如果 $x 和 $y 的值相同,则为 TRUE:

$x = 1; //int type
$y = "1"; //string type
if ($x == $y) {
    // This will execute
}

$x === $yTRUE if the value of the $x and $y are same and type of $x and $y are same:

$x === $y如果 $x 和 $y 的值相同且 $x 和 $y 的类型相同,则为 TRUE:

$x = 1; //int type
$y = "1"; //string type
if ($x === $y) {
    // This will not execute
}

回答by Keith

You'll see this operator in many dynamically typed languages, not just PHP.

您会在许多动态类型语言中看到这个运算符,而不仅仅是 PHP。

==will try to convert whatever it's dealing with into types that it can compare.

==将尝试将它处理的任何内容转换为它可以比较的类型。

===will strictly compare the type and value.

===将严格比较类型和值。

In any dynamically typed language you have to be careful with ==, you can get some interesting bugs.

在任何动态类型语言中,你都必须小心 ==,你可能会遇到一些有趣的错误。

The ternary ===is less convenient, but it's safer. For comparisons you should always give some additional thought to whether it should be ===or ==

三元===不太方便,但更安全。对于比较,您应该始终考虑是否应该=====

回答by Jon

The triple equals sign === checks to see whether two variables are equal and of the same type.

三重等号 === 检查两个变量是否相等且类型相同。

回答by umar

See Double and Triple equals operator in PHPthat I got for googling on "PHP three equals operator".

请参阅PHP中的Double 和 Triple 等于运算符,我在“PHP 三等于运算符”上进行了搜索。

At one point it says that:

有一次它说:

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.

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

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

It also gives an example to explain it.

它还给出了一个例子来解释它。

回答by Extrakun

For PHP, there many different meanings a zero can take

对于 PHP,零可以具有多种不同的含义

  1. it can be a Boolean false
  2. it could be a null value
  3. It could really be a zero
  1. 它可以是布尔值 false
  2. 它可能是一个空值
  3. 它真的可能是零

So === is added to ensure the type and the value are the same.

所以添加 === 以确保类型和值相同。

回答by Extrakun

"===" matching the value in the variable as well as data type of the variable.

“===”匹配变量中的值以及变量的数据类型。