{"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 \u2013 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[pastacode lang=\u201dphp\u201d manual=\u201d%3D%3D%20checks%20if%20equivalent%20(value%20only)%0A%0A%3D%3D%3D%20checks%20if%20the%20same%20(value%20%26%26%20type)%20%0A\u201d message=\u201dPHP code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"in-php\"><span style=\"color: #808000;\"><b>In PHP:<\/b><\/span><\/h4>\n<p>true == 1 (true \u2013 equivalent in value)<\/p>\n<p>true === 1 (false \u2013 not the same in value && type)<\/p>\n<p>true is boolean<\/p>\n<p>1 is int<\/p>\n<p>Here\u2019s a table we put together showing how some variables compare to each other.<\/p>\n<p>\/\/ \u201c===\u201d means that they are identical<\/p>\n<p>\/\/ \u201c==\u201d means that they are equal<\/p>\n<p>\/\/ \u201c!=\u201d means that they aren\u2019t 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=\u201dbanner\u201d]\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 !=, <> 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\u2019t 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[pastacode lang=\u201dphp\u201d manual=\u201d1%20%3D%3D%3D%201%3A%20true%0A1%20%3D%3D%201%3A%20true%0A1%20%3D%3D%3D%20%221%22%3A%20false%20%20%20%20%2F%2F%201%20is%20an%20integer%2C%20%221%22%20is%20a%20string%0A1%20%3D%3D%20%221%22%3A%20true%20%20%20%20%20%20%20%2F%2F%20%221%22%20gets%20casted%20to%20an%20integer%2C%20which%20is%201%0A%22foo%22%20%3D%3D%3D%20%22foo%22%3A%20true%20%20%20%2F%2F%20both%20operands%20are%20strings%20and%20have%20the%20same%20value%0A\u201d message=\u201dphp code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><b>Warning:<\/b> two instances of the same class with equivalent members do NOT match the === operator. Example:<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%24a%20%3D%20new%20stdClass()%3B%0A%24a-%3Efoo%20%3D%20%22bar%22%3B%0A%24b%20%3D%20clone%20%24a%3B%0Avar_dump(%24a%20%3D%3D%3D%20%24b)%3B%20%2F%2F%20bool(false%0A\u201d message=\u201djavascript code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><b>When should <\/b><b>we <\/b><b>use == and when ===? Here\u2019s an example.<\/b><\/p>\n<p>If we write<\/p>\n<p>if ($_REQUEST[name] == \u201c\u201d) \u2026..<\/p>\n<p>then \u00a0testing to see if a name has been entered on a form \u2013 it will return true if no name has been entered and it will also return true if there wasn\u2019t an input box on the form called \u201cname\u201d 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] === \u201c\u201d) \u2026..<\/p>\n<p>then this will return true if and only if the form had an input box called \u201cname\u201d into which the user didn\u2019t 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\u2019ve got a name or not (for whatever reason).<\/li>\n<li>That\u2019s why PHP has the flexibility and provides both == and ===<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\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}]}}