PHP 等式(== 双等式)和恒等式(=== 三等式)比较运算符有何不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/80646/
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
How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
提问by nickf
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?
什么是一些有用的例子?
回答by nickf
Difference between ==and ===
==和之间的区别===
The difference between the loosely ==equal operator and the strict ===identical operator is exactly explained in the manual:
松散==相等运算符和严格===相同运算符之间的区别在手册中有准确的解释:
Comparison Operators
┌──────────┬───────────┬───────────────────────────────────────────────────────────┐ │ Example │ Name │ Result │ ├──────────┼───────────┼───────────────────────────────────────────────────────────┤ │$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. │ └──────────┴───────────┴───────────────────────────────────────────────────────────┘
比较运算符
┌──────────┬───────────┬───────────────────────────────────────────────────────────┐ │ Example │ Name │ Result │ ├──────────┼───────────┼───────────────────────────────────────────────────────────┤ │$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. │ └──────────┴───────────┴───────────────────────────────────────────────────────────┘
Loosely ==equal comparison
松散==相等的比较
If you are using the ==operator, or any other comparison operator which uses loosely comparison such as !=, <>or ==, you always have to look at the contextto see what, where and why something gets converted to understand what is going on.
如果您正在使用==运算符,或任何其他使用松散比较的比较运算符,例如!=, <>or ==,您总是必须查看上下文以了解转换的内容、位置和原因,以了解正在发生的事情。
Converting rules
转换规则
- Converting to boolean
- Converting to integer
- Converting to float
- Converting to string
- Converting to array
- Converting to object
- Converting to resource
- Converting to NULL
Type comparison table
类型对照表
As reference and example you can see the comparison table in the manual:
作为参考和示例,您可以在手册中查看比较表:
Loose comparisons with
==┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐ │ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │ ├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤ │ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ │ 1 │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ 0 │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ │ -1 │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "1" │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "0" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "-1" │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ │ NULL │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ │ array() │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ FALSE │ │ "php" │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ │ "" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ └─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
松散的比较
==┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐ │ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │ ├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤ │ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ │ 1 │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ 0 │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ TRUE │ TRUE │ │ -1 │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "1" │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "0" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "-1" │ TRUE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ │ NULL │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ TRUE │ │ array() │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ TRUE │ FALSE │ FALSE │ │ "php" │ TRUE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ │ "" │ FALSE │ TRUE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ TRUE │ └─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
Strict ===identical comparison
严格===相同比较
If 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 magicallychange, 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.
如果您正在使用===运算符或任何其他使用严格比较的比较运算符,例如!==or ===,那么您始终可以确保类型不会神奇地改变,因为不会进行转换。因此,通过严格比较,类型和值必须相同,而不仅仅是值。
Type comparison table
类型对照表
As reference and example you can see the comparison table in the manual:
作为参考和示例,您可以在手册中查看比较表:
Strict comparisons with
===┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐ │ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │ ├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤ │ TRUE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ 1 │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ 0 │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ -1 │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "0" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "-1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ │ NULL │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ │ array() │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ │ "php" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ │ "" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ └─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
严格比较
===┌─────────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬───────┬─────────┬───────┬───────┐ │ │ TRUE │ FALSE │ 1 │ 0 │ -1 │ "1" │ "0" │ "-1" │ NULL │ array() │ "php" │ "" │ ├─────────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼───────┼─────────┼───────┼───────┤ │ TRUE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ 1 │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ 0 │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ -1 │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "0" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ │ "-1" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ FALSE │ │ NULL │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ FALSE │ │ array() │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ FALSE │ │ "php" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ FALSE │ │ "" │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ FALSE │ TRUE │ └─────────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴───────┴─────────┴───────┴───────┘
回答by Patrick Glandien
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.
如果两个不同的类型不同,运算符 == 会在它们之间进行转换,而 === 运算符执行“类型安全比较”。这意味着只有当两个操作数具有相同的类型和相同的值时,它才会返回 true。
Examples:
例子:
1 === 1: true
1 == 1: true
1 === "1": false // 1 is an integer, "1" is a string
1 == "1": true // "1" gets casted to an integer, which is 1
"foo" === "foo": true // both operands are strings and have the same value
Warning: two instances of the same class with equivalent members do NOT match the ===operator. Example:
警告:具有等效成员的同一类的两个实例与===运算符不匹配。例子:
$a = new stdClass();
$a->foo = "bar";
$b = clone $a;
var_dump($a === $b); // bool(false)
回答by Eric Leschinski
A picture is worth a thousand words:
一张图片胜过千言万语:
PHP Double Equals ==equality chart:
PHP Double Equals==等式图表:
PHP Triple Equals ===Equality chart:
PHP Triple Equals ===Equality 图表:
Source code to create these images:
创建这些图像的源代码:
https://github.com/sentientmachine/php_equality_charts
https://github.com/sentientmachine/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.
那些希望保持理智的人不要再读下去了,因为这些都没有任何意义,除了说这就是 PHP 的疯狂分形是如何设计的。
NAN != NANbutNAN == 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".When numbers are big enough they are == Infinity.
A fresh class is == to 1.
- False is the most dangerous value because False is == to most of the other variables, mostly defeating it's purpose.
NAN != NAN但是NAN == true。==如果 left 是数字,则将左右操作数转换为数字。所以123 == "123foo",但是"123" != "123foo"引号中的十六进制字符串有时是一个浮点数,并且会意外地强制转换为违反您的意愿的浮点数,从而导致运行时错误。
==不是可传递的,因为"0"== 0,0 == ""但是"0" != ""- 尚未声明的 PHP 变量是假的,即使 PHP 有一种表示未定义变量的方法,但该功能已通过
==. "6" == " 6","4.2" == "4.20", 和"133" == "0133"但是133 != 0133。但是"0x10" == "16","1e3" == "1000"将意外的字符串转换为八进制会在没有您的指示或同意的情况下发生,从而导致运行时错误。False == 0、""、[]和"0"。当数字足够大时,它们是 == Infinity。
一个新的类是 == 到 1。
- False 是最危险的值,因为 False 对于大多数其他变量来说是 ==,主要是违背了它的目的。
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 infinity that they are cast to infinity. With double equals, anything can be surprise ==to anything or, or can be surprise casted against your will and !=to something of which it should obviously be equal.
如果您使用 PHP,则不应使用双等号运算符,因为如果您使用三等号,唯一需要担心的边缘情况是 NAN 和非常接近无穷大的数字,以至于它们被强制转换为无穷大。使用双重等于,任何事物都可以==对任何事物感到惊讶,或者可以违背您的意愿并且!=对显然应该相等的事物感到惊讶。
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.
你==在 PHP 中使用的任何地方都是一种糟糕的代码气味,因为它中的 85 个错误被隐式转换规则暴露出来,这些规则似乎是由数百万程序员通过布朗运动编程设计的。
回答by Eric Leschinski
In regards to JavaScript:
关于 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'.
例如,下面的示例将显示“x 和 y 相等”,但不会显示“x 和 y 相同”。
var x = 4;
var y = '4';
if (x == y) {
alert('x and y are equal');
}
if (x === y) {
alert('x and y are identical');
}
回答by soulmerge
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 == $byields true.
== 使用对象的名称和它们的值来比较对象。如果两个对象具有相同的类型并且具有相同的成员值,则$a == $b产生 true。
=== compares the internal object id of the objects. Even if the members are equal, $a !== $bif they are not exactly the same object.
=== 比较对象的内部对象 id。即使成员相等,$a !== $b如果它们不是完全相同的对象。
class TestClassA {
public $a;
}
class TestClassB {
public $a;
}
$a1 = new TestClassA();
$a2 = new TestClassA();
$b = new TestClassB();
$a1->a = 10;
$a2->a = 10;
$b->a = 10;
$a1 == $a1;
$a1 == $a2; // Same members
$a1 != $b; // Different classes
$a1 === $a1;
$a1 !== $a2; // Not the same object
回答by silver
In simplest terms:
简单来说:
== checks if equivalent(value only)
== 检查是否相等(仅值)
=== checks if the same(value && type)
Equivalent vs. Same: An Analogy
=== 检查是否相同(值 && 类型)
等价与相同:类比
1 + 1 = 2 + 0(equivalent)
1 + 1 = 2 + 0(等价)
1 + 1 = 1 + 1(same)
In PHP:
1 + 1 = 1 + 1(相同)
在 PHP 中:
true == 1(true - equivalent in value)
true == 1(true - 等价)
true === 1(false - not the same in value && type)
true === 1(false - 值&&类型不同)
- true is boolean
- 1 is int
- true 是布尔值
- 1 是整数
回答by fico7489
One example is that a database attribute can be null or "":
一个例子是数据库属性可以为 null 或 "":
$attributeFromArray = "";
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //true
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //false
$attributeFromArray = null;
if ($attributeFromArray == ""){} //true
if ($attributeFromArray === ""){} //false
if ($attributeFromArray == null){} //true
if ($attributeFromArray === null){} //true
回答by fico7489
It's all about data types. Take a BOOL(true or false) for example:
这都是关于数据类型的。以BOOL(真或假)为例:
truealso equals 1and
falsealso equals 0
true也等于1和
false也等于0
The ==does not care about the data types when comparing:
So if you had a variable that is 1 (which could also be true):
该==不关心比较时的数据类型:所以,如果你有一个变量,它是1(也可能是true):
$var=1;
$var=1;
And then compare with the ==:
然后与==:
if ($var == true)
{
echo"var is true";
}
But $vardoes not actually equal true, does it? It has the int value of 1instead, which in turn, is equal to true.
但$var实际上并不等于true,是吗?它的 int 值是1相反的,而它又等于 true。
With ===, the data types are checked to make sure the two variables/objects/whatever are using the same type.
使用===,检查数据类型以确保两个变量/对象/任何使用相同的类型。
So if I did
所以如果我做了
if ($var === true)
{
echo "var is true";
}
that condition would not be true, as $var !== trueit only == true(if you know what I mean).
那个条件不会是真的,因为$var !== true它只是== true(如果你知道我的意思)。
Why would you need this?
你为什么需要这个?
Simple - let's take a look at one of PHP's functions: array_search():
简单——让我们来看看 PHP 的一个功能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 an array_search()on a value that was stored in the first element of the array(which would have the array key of 0)....the array_search()function would return 0...which is equal to false..
该array_search()函数只是在数组中搜索一个值,并返回找到该值的元素的键。如果在数组中找不到该值,则返回false。但是,如果您array_search()对存储在数组第一个元素中的值(其数组键为0...)执行操作,该array_search()函数将返回 0...这等于 false...
So if you did:
所以如果你这样做:
$arr = array("name");
if (array_search("name", $arr) == false)
{
// This would return 0 (the key of the element the val was found
// in), but because we're using ==, we'll think the function
// actually returned false...when it didn't.
}
So, do you see how this could be an issue now?
那么,你现在看到这可能是一个问题了吗?
Most people don't use == falsewhen checking if a function returns false. Instead, they use the !. But actually, this is exactly the same as using ==false, so if you did:
大多数人== false在检查函数是否返回 false 时不使用。相反,他们使用!. 但实际上,这与 using 完全相同==false,因此如果您这样做:
$arr = array("name");
if (!array_search("name", $arr)) // This is the same as doing (array_search("name", $arr) == false)
So for things like that, you would use the ===instead, so that the data type is checked.
因此,对于此类情况,您可以使用===代替,以便检查数据类型。
回答by 2rahulsk
php == is a comparison operator which compares the value of the variables. But === compares the value and the data type.
php == 是一个比较运算符,用于比较变量的值。但是 === 比较的是值和数据类型。
For example,
例如,
<?php
$var1 = 10;
$var2 = '10';
if($var1 == $var2) {
echo 'Variables are equal';
} else {
echo 'Variables are not equal';
}
?>
In this case the output will be 'Variables are equal', even though their data types are different.
在这种情况下,输出将是“变量相等”,即使它们的数据类型不同。
But if we use === instead of ==, the output will be 'Variables are not equal'. The php first compares the value of the variable and then the data type. Here the values are same, but data types are different.
但是如果我们使用 === 而不是 ==,输出将是“变量不相等”。php首先比较变量的值,然后比较数据类型。这里的值相同,但数据类型不同。
回答by Mannusanghi
Given x = 5
给定的 x = 5
1) Operator : == is "equal to". x == 8is false
2) Operator : === is "exactly equal to" (value and type) x === 5is true, x === "5"is false
1) 运算符 : == 是“等于”。x == 8为假
2) 运算符:=== 是“完全等于”(值和类型)x === 5为真,x === "5"为假


