<p>What is the difference between <code>==</code> and <code>===</code>?</p>
<ul>
<li>How exactly does the loosely <code>==</code> comparison work?</li>
<li>How exactly does the strict <code>===</code> comparison work?</li>
</ul>
<p>What would be some useful examples?</p>

[{'answer_id': 80649, 'author': 'nickf', 'author_id': 9021, 'author_profile': 'https://Stackoverflow.com/users/9021', 'pm_score': 10, 'selected': True, 'text': '<h2>Difference between <code>==</code> and <code>===</code></h2>\n<p>The difference between the loosely <code>==</code> equal operator and the strict <code>===</code> identical operator is exactly explained in the <a href="http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison" rel="nofollow noreferrer">manual</a>:</p>\n<p>Comparison Operators</p>\n<div class="s-table-container">\n<table class="s-table">\n<thead>\n<tr>\n<th>Example</th>\n<th>Name</th>\n<th>Result</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>$a ==  $b</td>\n<td>Equal</td>\n<td>TRUE if $a is equal to $b after type juggling.</td>\n</tr>\n<tr>\n<td>$a === $b</td>\n<td>Identical</td>\n<td>TRUE if $a is equal to $b, and they are of the same type.</td>\n</tr>\n</tbody>\n</table>\n</div><hr />\n<h2>Loosely <code>==</code> equal comparison</h2>\n<p>If you are using the <code>==</code> operator, or any other comparison operator which uses loosely comparison such as <code>!=</code>, <code>&lt;&gt;</code> or <code>==</code>, you always have to look at the <strong>context</strong> to see what, where and why something gets converted to understand what is going on.</p>\n<h2>Converting rules</h2>\n<ul>\n<li><a href="http://php.net/manual/en/language.types.boolean.php#language.types.boolean.casting" rel="nofollow noreferrer">Converting to boolean</a></li>\n<li><a href="http://php.net/manual/en/language.types.integer.php#language.types.integer.casting" rel="nofollow noreferrer">Converting to integer</a></li>\n<li><a href="http://php.net/manual/en/language.types.float.php#language.types.float.casting" rel="nofollow noreferrer">Converting to float</a></li>\n<li><a href="http://php.net/manual/en/language.types.string.php#language.types.string.casting" rel="nofollow noreferrer">Converting to string</a></li>\n<li><a href="http://php.net/manual/en/language.types.array.php#language.types.array.casting" rel="nofollow noreferrer">Converting to array</a></li>\n<li><a href="http://php.net/manual/en/language.types.object.php#language.types.object.casting" rel="nofollow noreferrer">Converting to object</a></li>\n<li><a href="http://php.net/manual/en/language.types.resource.php#language.types.resource.casting" rel="nofollow noreferrer">Converting to resource</a></li>\n<li><a href="http://php.net/manual/en/language.types.null.php#language.types.null.casting" rel="nofollow noreferrer">Converting to NULL</a></li>\n</ul>\n<h2>Type comparison table</h2>\n<p>As reference and example you can see the comparison table in the <a href="http://php.net/manual/en/types.comparisons.php#types.comparisions-loose" rel="nofollow noreferrer">manual</a>:</p>\n<div class="s-table-container">\n<table class="s-table">\n<thead>\n<tr>\n<th></th>\n<th>TRUE</th>\n<th>FALSE</th>\n<th>1</th>\n<th>0</th>\n<th>-1</th>\n<th>&quot;1&quot;</th>\n<th>&quot;0&quot;</th>\n<th>&quot;-1&quot;</th>\n<th>NULL</th>\n<th>array()</th>\n<th>&quot;php&quot;</th>\n<th>&quot;&quot;</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>TRUE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n</tr>\n<tr>\n<td>1</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>0</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>TRUE</td>\n</tr>\n<tr>\n<td>-1</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;1&quot;</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;0&quot;</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;-1&quot;</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>NULL</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n</tr>\n<tr>\n<td>array()</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;php&quot;</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;&quot;</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n</tr>\n</tbody>\n</table>\n</div><h2>Strict <code>===</code> identical comparison</h2>\n<p>If you are using the <code>===</code> operator, or any other comparison operator which uses strict comparison such as <code>!==</code> or <code>===</code>, then you can always be sure that the types won\'t <em>magically</em> change, 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.</p>\n<h2>Type comparison table</h2>\n<p>As reference and example you can see the comparison table in the <a href="http://php.net/manual/en/types.comparisons.php#type.comparisons-strict" rel="nofollow noreferrer">manual</a>:</p>\n<p>Strict comparisons with <code>===</code></p>\n<div class="s-table-container">\n<table class="s-table">\n<thead>\n<tr>\n<th></th>\n<th>TRUE</th>\n<th>FALSE</th>\n<th>1</th>\n<th>0</th>\n<th>-1</th>\n<th>&quot;1&quot;</th>\n<th>&quot;0&quot;</th>\n<th>&quot;-1&quot;</th>\n<th>NULL</th>\n<th>array()</th>\n<th>&quot;php&quot;</th>\n<th>&quot;&quot;</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>TRUE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>1</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>0</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>-1</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;1&quot;</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;0&quot;</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;-1&quot;</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>NULL</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>array()</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;php&quot;</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n<td>FALSE</td>\n</tr>\n<tr>\n<td>&quot;&quot;</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>FALSE</td>\n<td>TRUE</td>\n</tr>\n</tbody>\n</table>\n</div>\n<blockquote>\n<p><strong>Editor\'s note</strong> - This was properly quoted previously, but is more readable as a markdown table. This is not plagiarism</p>\n</blockquote>\n'}, {'answer_id': 80694, 'author': 'Stacey Richards', 'author_id': 1142, 'author_profile': 'https://Stackoverflow.com/users/1142', 'pm_score': 2, 'selected': False, 'text': "<p>You would use === to test whether a function or variable is false rather than just equating to false (zero or an empty string).</p>\n\n<pre><code>$needle = 'a';\n$haystack = 'abc';\n$pos = strpos($haystack, $needle);\nif ($pos === false) {\n    echo $needle . ' was not found in ' . $haystack;\n} else {\n    echo $needle . ' was found in ' . $haystack . ' at location ' . $pos;\n}\n</code></pre>\n\n<p>In this case strpos would return 0 which would equate to false in the test</p>\n\n<pre><code>if ($pos == false)\n</code></pre>\n\n<p>or</p>\n\n<pre><code>if (!$pos)\n</code></pre>\n\n<p>which is not what you want here.</p>\n"}, {'answer_id': 80796, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': "<p>In regards to JavaScript:</p>\n\n<p>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.</p>\n\n<p>For example, the sample below will display 'x and y are equal', but not 'x and y are identical'.</p>\n\n<pre><code>var x = 4;\nvar y = '4';\nif (x == y) {\n    alert('x and y are equal');\n}\nif (x === y) {\n    alert('x and y are identical');\n}\n</code></pre>\n"}, {'answer_id': 80861, 'author': 'ofaurax', 'author_id': 15209, 'author_profile': 'https://Stackoverflow.com/users/15209', 'pm_score': 1, 'selected': False, 'text': '<p>Variables have a type and a value.</p>\n\n<ul>\n<li>$var = "test" is a string that contain "test"</li>\n<li>$var2 = 24 is an integer vhose value is 24.</li>\n</ul>\n\n<p>When you use these variables (in PHP), sometimes you don\'t have the good type.\nFor example, if you do</p>\n\n<pre><code>if ($var == 1) {... do something ...}\n</code></pre>\n\n<p>PHP have to convert ("to cast") $var to integer. In this case, "$var == 1" is true because any non-empty string is casted to 1.</p>\n\n<p>When using ===, you check that the value AND THE TYPE are equal, so "$var === 1" is false.</p>\n\n<p>This is useful, for example, when you have a function that can return false (on error) and 0 (result) :</p>\n\n<pre><code>if(myFunction() == false) { ... error on myFunction ... }\n</code></pre>\n\n<p>This code is wrong as if <code>myFunction()</code> returns 0, it is casted to false and you seem to have an error. The correct code is :</p>\n\n<pre><code>if(myFunction() === false) { ... error on myFunction ... }\n</code></pre>\n\n<p>because the test is that the return value "is a boolean and is false" and not "can be casted to false".</p>\n'}, {'answer_id': 589558, 'author': 'Patrick Glandien', 'author_id': 66760, 'author_profile': 'https://Stackoverflow.com/users/66760', 'pm_score': 8, 'selected': False, 'text': '<p>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.</p>\n\n<p>Examples:</p>\n\n<pre><code>1 === 1: true\n1 == 1: true\n1 === "1": false // 1 is an integer, "1" is a string\n1 == "1": true // "1" gets casted to an integer, which is 1\n"foo" === "foo": true // both operands are strings and have the same value</code></pre>\n\n<p><strong>Warning</strong>: two instances of the same class with equivalent members do NOT match the <code>===</code> operator. Example:</p>\n\n<pre><code>$a = new stdClass();\n$a-&gt;foo = "bar";\n$b = clone $a;\nvar_dump($a === $b); // bool(false)\n</code></pre>\n'}, {'answer_id': 589748, 'author': 'soulmerge', 'author_id': 44562, 'author_profile': 'https://Stackoverflow.com/users/44562', 'pm_score': 5, 'selected': False, 'text': '<p>An addition to the other answers concerning object comparison:</p>\n\n<p>== 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, <code>$a == $b</code> yields true.</p>\n\n<p>=== compares the internal object id of the objects. Even if the members are equal, <code>$a !== $b</code> if they are not exactly the same object.</p>\n\n<pre><code>class TestClassA {\n    public $a;\n}\n\nclass TestClassB {\n    public $a;\n}\n\n$a1 = new TestClassA();\n$a2 = new TestClassA();\n$b = new TestClassB();\n\n$a1-&gt;a = 10;\n$a2-&gt;a = 10;\n$b-&gt;a = 10;\n\n$a1 == $a1;\n$a1 == $a2;  // Same members\n$a1 != $b;   // Different classes\n\n$a1 === $a1;\n$a1 !== $a2; // Not the same object\n</code></pre>\n'}, {'answer_id': 11545695, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>It\'s all about data types. Take a <code>BOOL</code> (true or false) for example:</p>\n\n<p><code>true</code> also equals <code>1</code> and\n<code>false</code> also equals <code>0</code></p>\n\n<p>The <code>==</code> does not care about the data types when comparing:\nSo if you had a variable that is 1 (which could also be <code>true</code>):</p>\n\n<p><code>$var=1;</code></p>\n\n<p>And then compare with the <code>==</code>:</p>\n\n<pre><code>if ($var == true)\n{\n    echo"var is true";\n}\n</code></pre>\n\n<p>But <code>$var</code> does not actually equal <code>true</code>, does it? It has the int value of <code>1</code> instead, which in turn, is equal to true.</p>\n\n<p>With <code>===</code>, the data types are checked to make sure the two variables/objects/whatever are using the same type.</p>\n\n<p>So if I did</p>\n\n<pre><code>if ($var === true)\n{\n    echo "var is true";\n}\n</code></pre>\n\n<p>that condition would not be true, as <code>$var !== true</code> it only <code>== true</code> (if you know what I mean).</p>\n\n<p>Why would you need this?</p>\n\n<p>Simple - let\'s take a look at one of PHP\'s functions: <code>array_search()</code>:</p>\n\n<p>The <code>array_search()</code> 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 <strong>false</strong>. But, what if you did an <code>array_search()</code> on a value that was stored in the <strong>first element of the array</strong> (which would have the array key of <code>0</code>)....the <code>array_search()</code> function would return 0...which is equal to false..</p>\n\n<p>So if you did:</p>\n\n<pre><code>$arr = array("name");\nif (array_search("name", $arr) == false)\n{\n    // This would return 0 (the key of the element the val was found\n    // in), but because we\'re using ==, we\'ll think the function\n    // actually returned false...when it didn\'t.\n}\n</code></pre>\n\n<p>So, do you see how this could be an issue now?</p>\n\n<p>Most people don\'t use <code>== false</code> when checking if a function returns false. Instead, they use the <code>!</code>. But actually, this is exactly the same as using <code>==false</code>, so if you did:</p>\n\n<pre><code>$arr = array("name");\nif (!array_search("name", $arr)) // This is the same as doing (array_search("name", $arr) == false)\n</code></pre>\n\n<p>So for things like that, you would use the <code>===</code> instead, so that the data type is checked.</p>\n'}, {'answer_id': 32642207, 'author': 'Sathish', 'author_id': 1691723, 'author_profile': 'https://Stackoverflow.com/users/1691723', 'pm_score': 1, 'selected': False, 'text': '<pre><code>&lt;?php\n\n    /**\n     * Comparison of two PHP objects                         ==     ===\n     * Checks for\n     * 1. References                                         yes    yes\n     * 2. Instances with matching attributes and its values  yes    no\n     * 3. Instances with different attributes                yes    no\n     **/\n\n    // There is no need to worry about comparing visibility of property or\n    // method, because it will be the same whenever an object instance is\n    // created, however visibility of an object can be modified during run\n    // time using ReflectionClass()\n    // http://php.net/manual/en/reflectionproperty.setaccessible.php\n    //\n    class Foo\n    {\n        public $foobar = 1;\n\n        public function createNewProperty($name, $value)\n        {\n            $this-&gt;{$name} = $value;\n        }\n    }\n\n    class Bar\n    {\n    }\n    // 1. Object handles or references\n    // Is an object a reference to itself or a clone or totally a different object?\n    //\n    //   ==  true   Name of two objects are same, for example, Foo() and Foo()\n    //   ==  false  Name of two objects are different, for example, Foo() and Bar()\n    //   === true   ID of two objects are same, for example, 1 and 1\n    //   === false  ID of two objects are different, for example, 1 and 2\n\n    echo "1. Object handles or references (both == and    ===) &lt;br /&gt;";\n\n    $bar = new Foo();    // New object Foo() created\n    $bar2 = new Foo();   // New object Foo() created\n    $baz = clone $bar;   // Object Foo() cloned\n    $qux = $bar;         // Object Foo() referenced\n    $norf = new Bar();   // New object Bar() created\n    echo "bar";\n    var_dump($bar);\n    echo "baz";\n    var_dump($baz);\n    echo "qux";\n    var_dump($qux);\n    echo "bar2";\n    var_dump($bar2);\n    echo "norf";\n    var_dump($norf);\n\n    // Clone: == true and === false\n    echo \'$bar == $bar2\';\n    var_dump($bar == $bar2); // true\n\n    echo \'$bar === $bar2\';\n    var_dump($bar === $bar2); // false\n\n    echo \'$bar == $baz\';\n    var_dump($bar == $baz); // true\n\n    echo \'$bar === $baz\';\n    var_dump($bar === $baz); // false\n\n    // Object reference: == true and === true\n    echo \'$bar == $qux\';\n    var_dump($bar == $qux); // true\n\n    echo \'$bar === $qux\';\n    var_dump($bar === $qux); // true\n\n    // Two different objects: == false and === false\n    echo \'$bar == $norf\';\n    var_dump($bar == $norf); // false\n\n    echo \'$bar === $norf\';\n    var_dump($bar === $norf); // false\n\n    // 2. Instances with matching attributes and its values (only ==).\n    //    What happens when objects (even in cloned object) have same\n    //    attributes but varying values?\n\n    // $foobar value is different\n    echo "2. Instances with matching attributes  and its values (only ==) &lt;br /&gt;";\n\n    $baz-&gt;foobar = 2;\n    echo \'$foobar\' . " value is different &lt;br /&gt;";\n    echo \'$bar-&gt;foobar = \' . $bar-&gt;foobar . "&lt;br /&gt;";\n    echo \'$baz-&gt;foobar = \' . $baz-&gt;foobar . "&lt;br /&gt;";\n    echo \'$bar == $baz\';\n    var_dump($bar == $baz); // false\n\n    // $foobar\'s value is the same again\n    $baz-&gt;foobar = 1;\n    echo \'$foobar\' . " value is the same again &lt;br /&gt;";\n    echo \'$bar-&gt;foobar is \' . $bar-&gt;foobar . "&lt;br /&gt;";\n    echo \'$baz-&gt;foobar is \' . $baz-&gt;foobar . "&lt;br /&gt;";\n    echo \'$bar == $baz\';\n    var_dump($bar == $baz); // true\n\n    // Changing values of properties in $qux object will change the property\n    // value of $bar and evaluates true always, because $qux = &amp;$bar.\n    $qux-&gt;foobar = 2;\n    echo \'$foobar value of both $qux and $bar is 2, because $qux = &amp;$bar\' . "&lt;br /&gt;";\n    echo \'$qux-&gt;foobar is \' . $qux-&gt;foobar . "&lt;br /&gt;";\n    echo \'$bar-&gt;foobar is \' . $bar-&gt;foobar . "&lt;br /&gt;";\n    echo \'$bar == $qux\';\n    var_dump($bar == $qux); // true\n\n    // 3. Instances with different attributes (only ==)\n    //    What happens when objects have different attributes even though\n    //    one of the attributes has same value?\n    echo "3. Instances with different attributes (only ==) &lt;br /&gt;";\n\n    // Dynamically create a property with the name in $name and value\n    // in $value for baz object\n    $name = \'newproperty\';\n    $value = null;\n    $baz-&gt;createNewProperty($name, $value);\n    echo \'$baz-&gt;newproperty is \' . $baz-&gt;{$name};\n    var_dump($baz);\n\n    $baz-&gt;foobar = 2;\n    echo \'$foobar\' . " value is same again &lt;br /&gt;";\n    echo \'$bar-&gt;foobar is \' . $bar-&gt;foobar . "&lt;br /&gt;";\n    echo \'$baz-&gt;foobar is \' . $baz-&gt;foobar . "&lt;br /&gt;";\n    echo \'$bar == $baz\';\n    var_dump($bar == $baz); // false\n    var_dump($bar);\n    var_dump($baz);\n?&gt;\n</code></pre>\n'}, {'answer_id': 40392064, 'author': 'Eric Leschinski', 'author_id': 445131, 'author_profile': 'https://Stackoverflow.com/users/445131', 'pm_score': 7, 'selected': False, 'text': '<p>A picture is worth a thousand words:</p>\n<h2>PHP Double Equals <code>==</code> equality chart:</h2>\n<p><a href="https://github.com/sentientmachine/php_equality_charts" rel="noreferrer"><img src="https://i.stack.imgur.com/xI8W7.png" alt="enter image description here" /></a></p>\n<h2>PHP Triple Equals <code>===</code> Equality chart:</h2>\n<p><a href="https://github.com/sentientmachine/php_equality_charts" rel="noreferrer"><img src="https://i.stack.imgur.com/gs1Eh.png" alt="enter image description here" /></a></p>\n<p>Source code to create these images:</p>\n<p><a href="https://github.com/sentientmachine/php_equality_charts" rel="noreferrer">https://github.com/sentientmachine/php_equality_charts</a></p>\n<h2>Guru Meditation</h2>\n<p>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.</p>\n<ol>\n<li><p><code>NAN != NAN</code> but <code>NAN == true</code>.</p>\n</li>\n<li><p><code>==</code> will convert left and right operands to numbers if left is a number.  So <code>123 == &quot;123foo&quot;</code>, but <code>&quot;123&quot; != &quot;123foo&quot;</code></p>\n</li>\n<li><p>A hex string in quotes is occasionally a float, and will be surprise cast to float against your will, causing a runtime error.</p>\n</li>\n<li><p><code>==</code> is not transitive because <code>&quot;0&quot;== 0</code>, and <code>0 == &quot;&quot;</code> but <code>&quot;0&quot; != &quot;&quot;</code></p>\n</li>\n<li><p>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 <code>==</code>.</p>\n</li>\n<li><p><code>&quot;6&quot; == &quot; 6&quot;</code>, <code>&quot;4.2&quot; == &quot;4.20&quot;</code>, and <code>&quot;133&quot; == &quot;0133&quot;</code> but <code>133 != 0133</code>. But <code>&quot;0x10&quot; == &quot;16&quot;</code> and <code>&quot;1e3&quot; == &quot;1000&quot;</code> exposing that surprise string conversion to octal will occur both without your instruction or consent, causing a runtime error.</p>\n</li>\n<li><p><code>False == 0</code>, <code>&quot;&quot;</code>, <code>[]</code> and <code>&quot;0&quot;</code>.</p>\n</li>\n<li><p>If you add 1 to number and they are already holding their maximum value, they do not wrap around, instead they are cast to <code>infinity</code>.</p>\n</li>\n<li><p>A fresh class is == to 1.</p>\n</li>\n<li><p>False is the most dangerous value because False is == to most of the other variables, mostly defeating it\'s purpose.</p>\n</li>\n</ol>\n<h2>Hope:</h2>\n<p>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 their datatype\'s maximum value, that they are cast to infinity.  With double equals, anything can be surprise <code>==</code> to anything or, or can be surprise casted against your will and <code>!=</code> to something of which it should obviously be equal.</p>\n<p>Anywhere you use <code>==</code> 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.</p>\n'}, {'answer_id': 43857980, 'author': 'DavidWalley', 'author_id': 3298321, 'author_profile': 'https://Stackoverflow.com/users/3298321', 'pm_score': 1, 'selected': False, 'text': '<p>All of the answers so far ignore a dangerous problem with ===. It has been noted in passing, but not stressed, that integer and double are different types, so the following code:</p>\n\n<pre><code>$n = 1000;\n$d = $n + 0.0e0;\necho \'&lt;br/&gt;\'. ( ($n ==  $d)?\'equal\' :\'not equal\' );\necho \'&lt;br/&gt;\'. ( ($n === $d)?\'equal\' :\'not equal\' );\n</code></pre>\n\n<p>gives:</p>\n\n<pre><code> equal\n not equal\n</code></pre>\n\n<p>Note that this is NOT a case of a "rounding error". The two numbers are exactly equal down to the last bit, but they have different types.</p>\n\n<p>This is a nasty problem because a program using === can run happily for years if all of the numbers are small enough (where "small enough" depends on the hardware and OS you are running on). However, if by chance, an integer happens to be large enough to be converted to a double, its type is changed "forever" even though a subsequent operation, or many operations, might bring it back to a small integer in value. And, it gets worse. It can spread - double-ness infection can be passed along to anything it touches, one calculation at a time. </p>\n\n<p>In the real world, this is likely to be a problem in programs that handle dates beyond the year 2038, for example. At this time, UNIX timestamps (number of seconds since 1970-01-01 00:00:00 UTC) will require more than 32-bits, so their representation will "magically" switch to double on some systems. Therefore, if you calculate the difference between two times you might end up with a couple of seconds, but as a double, rather than the integer result that occurs in the year 2017.</p>\n\n<p>I think this is much worse than conversions between strings and numbers because it is subtle. I find it easy to keep track of what is a string and what is a number, but keeping track of the number of bits in a number is beyond me.</p>\n\n<p>So, in the above answers there are some nice tables, but no distinction between 1 (as an integer) and 1 (subtle double) and 1.0 (obvious double). Also, advice that you should always use === and never == is not great because === will sometimes fail where == works properly. Also, JavaScript is not equivalent in this regard because it has only one number type (internally it may have different bit-wise representations, but it does not cause problems for ===).</p>\n\n<p>My advice - use neither. You need to write your own comparison function to really fix this mess.</p>\n'}, {'answer_id': 52299551, 'author': 'MAChitgarha', 'author_id': 4215651, 'author_profile': 'https://Stackoverflow.com/users/4215651', 'pm_score': 1, 'selected': False, 'text': '<p>There are two differences between <code>==</code> and <code>===</code> in PHP arrays and objects that nobody mentioned: two arrays with different key sorts, and objects.</p>\n<h3>Two arrays with different key sorts</h3>\n<p>If you have two arrays with their keys sorted differently, but having equal key-value maps, they are strictly different (i.e. using <code>===</code>). That might lead to problems, if you key-sort an array, and try to compare the sorted array with the original one.</p>\n<p>For example:</p>\n<pre class="lang-php prettyprint-override"><code>$arrayUnsorted = [\n    &quot;you&quot; =&gt; &quot;you&quot;,\n    &quot;I&quot; =&gt; &quot;we&quot;,\n];\n\n$arraySorted = $arrayUnsorted;\nksort($arraySorted);\n\n$arrayUnsorted == $arraySorted; // true\n$arrayUnsorted === $arraySorted; // false\n</code></pre>\n<h3>Objects</h3>\n<p>Keep in mind, the main rule is that <em>two different objects are never strict-equal</em>. Look at the following example:</p>\n<pre class="lang-php prettyprint-override"><code>$stdClass1 = new stdClass();\n$stdClass2 = new stdClass();\n$clonedStdClass1 = clone $stdClass1;\n\n$stdClass1 == $stdClass2; // true\n$stdClass1 === $stdClass2; // false\n$stdClass1 == $clonedStdClass1; // true\n$stdClass1 === $clonedStdClass1; // false\n</code></pre>\n<p><strong>Note</strong>: Assigning an object to another variable does not create a copy - rather, it creates a reference to the same object. <a href="https://stackoverflow.com/a/2144654/4215651">See here</a>.</p>\n<p><strong>Note</strong>: As of PHP7, <a href="http://www.php.net/manual/en/language.oop5.anonymous.php" rel="nofollow noreferrer">anonymous classes</a> was introduced. There is no difference between a <code>new class {}</code> and a <code>new stdClass()</code> in the tests above.</p>\n'}, {'answer_id': 65644706, 'author': 'thomas', 'author_id': 12903396, 'author_profile': 'https://Stackoverflow.com/users/12903396', 'pm_score': 2, 'selected': False, 'text': '<h2>PHP Double Equals <code>==</code> :</h2>\n<p>In most programming languages, the comparison operator (==) checks, on the one hand, the data type and on the other hand the content of the variable for equality. The standard comparison operator (==) in PHP behaves differently. This tries to convert both variables into the same data type before the comparison and only then checks whether the content of these variables is the same. The following results are obtained:</p>\n<pre><code>&lt;?php\n    var_dump( 1 == 1 );     // true\n    var_dump( 1 == \'1\' );   // true\n    var_dump( 1 == 2 );     // false\n    var_dump( 1 == \'2\' );   // false\n    var_dump( 1 == true );  // true\n    var_dump( 1 == false ); // false\n?&gt;\n</code></pre>\n<h2>PHP Triple Equals <code>===</code> :</h2>\n<p>This operator also checks the datatype of the variable and returns (bool)true only if both variables have the same content and the same datatype. The following would therefore be correct:</p>\n<pre><code>&lt;?php\n    var_dump( 1 === 1 );     // true\n    var_dump( 1 === \'1\' );   // false\n    var_dump( 1 === 2 );     // false\n    var_dump( 1 === \'2\' );   // false\n    var_dump( 1 === true );  // false\n    var_dump( 1 === false ); // false\n?&gt;\n</code></pre>\n<p>Read more in <a href="https://stackhowto.com/what-is-the-difference-between-equal-and-identity-operator-in-php/" rel="nofollow noreferrer">What is the difference between == and === in PHP</a></p>\n'}, {'answer_id': 66167669, 'author': 'Gufran Hasan', 'author_id': 3041435, 'author_profile': 'https://Stackoverflow.com/users/3041435', 'pm_score': 1, 'selected': False, 'text': "<p><strong>Difference between == (equal) and === (identical equal)</strong></p>\n<p>PHP provides two comparison operators to check equality of two values. The main difference between of these two is that <code>'=='</code> checks if the values of the two operands are <code>equal or not</code>. On the other hand, <code>'==='</code> checks the values as well as the type of operands are <code>equal or not</code>.</p>\n<blockquote>\n<p>== (Equal)</p>\n<p>=== (Identical equal)</p>\n</blockquote>\n<p><strong>Example =&gt;</strong></p>\n<pre><code>&lt;?php \n    $val1 = 1234;\n    $val2 = &quot;1234&quot;;\n    var_dump($val1 == $val2);// output =&gt; bool(true)\n    //It checks only operands value\n?&gt; \n\n\n&lt;?php \n    $val1 = 1234;\n    $val2 = &quot;1234&quot;;\n    var_dump($val1 === $val2);// output =&gt; bool(false)\n    //First it checks type then operands value\n?&gt; \n</code></pre>\n<p>if we type cast $val2 to (int)$val2 or (string)$val1 then it returns true</p>\n<pre><code>   &lt;?php \n        $val1 = 1234;\n        $val2 = &quot;1234&quot;;\n        var_dump($val1 === (int)$val2);// output =&gt; bool(true)\n        //First it checks type then operands value\n    ?&gt; \n</code></pre>\n<p>OR</p>\n<pre><code>  &lt;?php \n        $val1 = 1234;\n        $val2 = &quot;1234&quot;;\n        var_dump($val1 === (int)$val2);// output =&gt; bool(true)\n        //First it checks type then operands value\n    ?&gt; \n</code></pre>\n"}]
['https://Stackoverflow.com/questions/80646', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9021/']