{"id":1291,"date":"2017-03-20T19:09:07","date_gmt":"2017-03-20T13:39:07","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1291"},"modified":"2017-03-29T11:39:24","modified_gmt":"2017-03-29T06:09:24","slug":"php-equality-double-equals-identity-triple-equals-comparison-operators-differ","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/php-equality-double-equals-identity-triple-equals-comparison-operators-differ\/","title":{"rendered":"How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ"},"content":{"rendered":"<h4 id=\"double-and-triple-equals-operator-in-php\"><span style=\"color: #ff6600;\"><b>Double and Triple equals operator in PHP<\/b><\/span><\/h4>\n<ul>\n<li>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.<\/li>\n<li>A triple = sign is a comparison to see whether two variables \/ expressions \/ constants are equal AND have the same type &#8211; i.e. both are strings or both are integers.<\/li>\n<\/ul>\n<h4 id=\"in-simplest-terms\"><span style=\"color: #800080;\"><b>In simplest terms:<\/b><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">PHP code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">== checks if equivalent (value only)<br\/><br\/>=== checks if the same (value &amp;&amp; type) <\/code><\/pre> <\/div>\n<h4 id=\"in-php\"><span style=\"color: #808000;\"><b>In PHP:<\/b><\/span><\/h4>\n<p>true == 1 (true &#8211; equivalent in value)<\/p>\n<p>true === 1 (false &#8211; not the same in value &amp;&amp; type)<\/p>\n<p>true is boolean<\/p>\n<p>1 is int<\/p>\n<p>Here&#8217;s a table we put together showing how some variables compare to each other.<\/p>\n<p>\/\/ &#8220;===&#8221; means that they are identical<\/p>\n<p>\/\/ &#8220;==&#8221; means that they are equal<\/p>\n<p>\/\/ &#8220;!=&#8221; means that they aren&#8217;t equal.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-1300 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/2017-03-20_175121.png\" alt=\"\" width=\"571\" height=\"230\" \/><\/p>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"difference-between-and\"><span style=\"color: #ff6600;\"><b>Difference between == and ===<\/b><\/span><\/h4>\n<p><span style=\"color: #808000;\"><b>Comparison <\/b><b>Operators:-<\/b><\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1293 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/2017-03-20_174517.png\" alt=\"\" width=\"625\" height=\"181\" \/><\/p>\n<h4 id=\"loosely-equal-comparison\"><span style=\"color: #800080;\"><b>Loosely == equal <\/b><b>comparison:<\/b><\/span><\/h4>\n<ul>\n<li>If you are using the == operator, or any other comparison operator which uses loosely comparison such as !=, &lt;&gt; or ==, you always have to look at the context to see what, where and why something gets converted to understand what is going on.<\/li>\n<\/ul>\n<h4 id=\"strict-identical-comparison\"><span style=\"color: #808000;\"><b>Strict === identical <\/b><b>comparison:<\/b><\/span><\/h4>\n<ul>\n<li>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&#8217;t magically change, because there will be no converting going on.<\/li>\n<li>So with strict comparison the type and value have to be the same, not only the value.<\/li>\n<\/ul>\n<p><b>Example:<\/b><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">1 === 1: true<br\/>1 == 1: true<br\/>1 === &quot;1&quot;: false    \/\/ 1 is an integer, &quot;1&quot; is a string<br\/>1 == &quot;1&quot;: true       \/\/ &quot;1&quot; gets casted to an integer, which is 1<br\/>&quot;foo&quot; === &quot;foo&quot;: true   \/\/ both operands are strings and have the same value<\/code><\/pre> <\/div>\n<p><b>Warning:<\/b> two instances of the same class with equivalent members do NOT match the === operator. Example:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">javascript code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">$a = new stdClass();<br\/>$a-&gt;foo = &quot;bar&quot;;<br\/>$b = clone $a;<br\/>var_dump($a === $b); \/\/ bool(false<\/code><\/pre> <\/div>\n<p><b>When should <\/b><b>we <\/b><b>use == and when ===? Here&#8217;s an example.<\/b><\/p>\n<p>If we write<\/p>\n<p>if ($_REQUEST[name] == &#8220;&#8221;) &#8230;..<\/p>\n<p>then \u00a0testing to see if a name has been entered on a form &#8211; it will return true if no name has been entered and it will also return true if there wasn&#8217;t an input box on the form called &#8220;name&#8221; or if the URL was called up without a form at all. However, if we use the three-equals varient:<\/p>\n<p>if ($_REQUEST[name] === &#8220;&#8221;) &#8230;..<\/p>\n<p>then this will return true if and only if the form had an input box called &#8220;name&#8221; into which the user didn&#8217;t type anything at all.<\/p>\n<ul>\n<li>Naturally, there will be times when you want to check very specifically that the form was submitted but with an empty box, and other times where you want to check simply if you&#8217;ve got a name or not (for whatever reason).<\/li>\n<li>That&#8217;s why PHP has the flexibility and provides both == and ===<\/li>\n<\/ul>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Double and Triple equals operator in PHP 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 \/ expressions \/ [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[2528,2529,2520,2524,2527,2521,2522,2538,2535,2531,2534,2532,2537,2530,2533,2536,404,2523,2525,2526,2519],"class_list":["post-1291","post","type-post","status-publish","format-standard","hentry","category-php","tag-2528","tag-and","tag-difference-between-and-in-javascript","tag-double-not-operator-in-php","tag-eql","tag-php-vs-operator","tag-php-comparison-operators-and-data-types","tag-php-conditional-operator","tag-php-double-colon-vs-arrow","tag-php-equals-string","tag-php-if-equals-string","tag-php-logical-operators","tag-php-open-tag","tag-php-querycomparison-method-with-operators","tag-php-single-quote-vs-double-quote","tag-php-type-juggling","tag-reference-what-does-this-symbol-mean-in-php","tag-the-3-different-equals","tag-whats-the-difference-between-equal-and-identical-comparison-operators-in-php","tag-whats-the-difference-between-equal","tag-which-equals-operator-vs-should-be-used-in-javascript-comparisons"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1291"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1291\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}