==和===的区别是什么?
松==比较究竟是如何工作的? 严格的===比较到底是如何工作的?
有什么有用的例子吗?
==和===的区别是什么?
松==比较究竟是如何工作的? 严格的===比较到底是如何工作的?
有什么有用的例子吗?
当前回答
变量有类型和值。
$var = "test"是一个包含"test"的字符串 $var2 = 24是一个整数,其值为24。
当您使用这些变量(在PHP中)时,有时您没有好的类型。 例如,如果你这样做
if ($var == 1) {... do something ...}
PHP必须将$var转换为整数。在本例中,"$var == 1"为真,因为任何非空字符串都被转换为1。
当使用===时,检查值和类型是否相等,因此"$var === 1"为假。
这很有用,例如,当你有一个函数可以返回false(错误)和0(结果):
if(myFunction() == false) { ... error on myFunction ... }
这段代码是错误的,如果myFunction()返回0,它被强制转换为false,你似乎有一个错误。正确的代码是:
if(myFunction() === false) { ... error on myFunction ... }
因为测试的是返回值“是一个布尔值并且为假”,而不是“可以被强制转换为假”。
其他回答
PHP双等号==:
在大多数编程语言中,比较操作符(==)一方面检查数据类型,另一方面检查变量的内容是否相等。PHP中的标准比较运算符(==)表现不同。这将尝试在比较之前将两个变量转换为相同的数据类型,然后才检查这些变量的内容是否相同。得到以下结果:
<?php
var_dump( 1 == 1 ); // true
var_dump( 1 == '1' ); // true
var_dump( 1 == 2 ); // false
var_dump( 1 == '2' ); // false
var_dump( 1 == true ); // true
var_dump( 1 == false ); // false
?>
PHP三重等于===:
此操作符还检查变量的数据类型,仅当两个变量具有相同的内容和相同的数据类型时才返回(bool)true。因此,以下是正确的:
<?php
var_dump( 1 === 1 ); // true
var_dump( 1 === '1' ); // false
var_dump( 1 === 2 ); // false
var_dump( 1 === '2' ); // false
var_dump( 1 === true ); // false
var_dump( 1 === false ); // false
?>
更多内容请参见PHP中==和===的区别
变量有类型和值。
$var = "test"是一个包含"test"的字符串 $var2 = 24是一个整数,其值为24。
当您使用这些变量(在PHP中)时,有时您没有好的类型。 例如,如果你这样做
if ($var == 1) {... do something ...}
PHP必须将$var转换为整数。在本例中,"$var == 1"为真,因为任何非空字符串都被转换为1。
当使用===时,检查值和类型是否相等,因此"$var === 1"为假。
这很有用,例如,当你有一个函数可以返回false(错误)和0(结果):
if(myFunction() == false) { ... error on myFunction ... }
这段代码是错误的,如果myFunction()返回0,它被强制转换为false,你似乎有一个错误。正确的代码是:
if(myFunction() === false) { ... error on myFunction ... }
因为测试的是返回值“是一个布尔值并且为假”,而不是“可以被强制转换为假”。
<?php
/**
* Comparison of two PHP objects == ===
* Checks for
* 1. References yes yes
* 2. Instances with matching attributes and its values yes no
* 3. Instances with different attributes yes no
**/
// There is no need to worry about comparing visibility of property or
// method, because it will be the same whenever an object instance is
// created, however visibility of an object can be modified during run
// time using ReflectionClass()
// http://php.net/manual/en/reflectionproperty.setaccessible.php
//
class Foo
{
public $foobar = 1;
public function createNewProperty($name, $value)
{
$this->{$name} = $value;
}
}
class Bar
{
}
// 1. Object handles or references
// Is an object a reference to itself or a clone or totally a different object?
//
// == true Name of two objects are same, for example, Foo() and Foo()
// == false Name of two objects are different, for example, Foo() and Bar()
// === true ID of two objects are same, for example, 1 and 1
// === false ID of two objects are different, for example, 1 and 2
echo "1. Object handles or references (both == and ===) <br />";
$bar = new Foo(); // New object Foo() created
$bar2 = new Foo(); // New object Foo() created
$baz = clone $bar; // Object Foo() cloned
$qux = $bar; // Object Foo() referenced
$norf = new Bar(); // New object Bar() created
echo "bar";
var_dump($bar);
echo "baz";
var_dump($baz);
echo "qux";
var_dump($qux);
echo "bar2";
var_dump($bar2);
echo "norf";
var_dump($norf);
// Clone: == true and === false
echo '$bar == $bar2';
var_dump($bar == $bar2); // true
echo '$bar === $bar2';
var_dump($bar === $bar2); // false
echo '$bar == $baz';
var_dump($bar == $baz); // true
echo '$bar === $baz';
var_dump($bar === $baz); // false
// Object reference: == true and === true
echo '$bar == $qux';
var_dump($bar == $qux); // true
echo '$bar === $qux';
var_dump($bar === $qux); // true
// Two different objects: == false and === false
echo '$bar == $norf';
var_dump($bar == $norf); // false
echo '$bar === $norf';
var_dump($bar === $norf); // false
// 2. Instances with matching attributes and its values (only ==).
// What happens when objects (even in cloned object) have same
// attributes but varying values?
// $foobar value is different
echo "2. Instances with matching attributes and its values (only ==) <br />";
$baz->foobar = 2;
echo '$foobar' . " value is different <br />";
echo '$bar->foobar = ' . $bar->foobar . "<br />";
echo '$baz->foobar = ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
// $foobar's value is the same again
$baz->foobar = 1;
echo '$foobar' . " value is the same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // true
// Changing values of properties in $qux object will change the property
// value of $bar and evaluates true always, because $qux = &$bar.
$qux->foobar = 2;
echo '$foobar value of both $qux and $bar is 2, because $qux = &$bar' . "<br />";
echo '$qux->foobar is ' . $qux->foobar . "<br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$bar == $qux';
var_dump($bar == $qux); // true
// 3. Instances with different attributes (only ==)
// What happens when objects have different attributes even though
// one of the attributes has same value?
echo "3. Instances with different attributes (only ==) <br />";
// Dynamically create a property with the name in $name and value
// in $value for baz object
$name = 'newproperty';
$value = null;
$baz->createNewProperty($name, $value);
echo '$baz->newproperty is ' . $baz->{$name};
var_dump($baz);
$baz->foobar = 2;
echo '$foobar' . " value is same again <br />";
echo '$bar->foobar is ' . $bar->foobar . "<br />";
echo '$baz->foobar is ' . $baz->foobar . "<br />";
echo '$bar == $baz';
var_dump($bar == $baz); // false
var_dump($bar);
var_dump($baz);
?>
关于JavaScript:
===操作符的工作原理与==操作符相同,但它要求其操作数不仅具有相同的值,而且具有相同的数据类型。
例如,下面的示例将显示'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');
}
一图胜千言万语:
PHP双等号==等号图表:
PHP三重等号===等号图表:
创建这些图像的源代码:
https://github.com/sentientmachine/php_equality_charts
大师冥想
那些希望保持理智的人,不要再读下去了,因为这些都没有任何意义,只能说PHP的疯狂分形就是这样设计的。
NAN != NAN but NAN == true. == will convert left and right operands to numbers if left is a number. So 123 == "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, and 0 == "" 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" but 133 != 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 it's purpose.
希望:
如果你正在使用PHP,你不应该使用双等号操作符,因为如果你使用三等号,唯一需要担心的边缘情况是NAN和太接近其数据类型最大值的数字,它们会被强制转换为无穷大。使用双等号,任何东西都可以是surprise ==对任何东西或,或可以是出乎你的意愿和!=对显然应该是相等的东西。
在PHP中使用==的任何地方都是糟糕的代码气味,因为隐式强制转换规则暴露了85个错误,这些规则似乎是由数百万程序员通过布朗运动编程设计的。