{"id":99,"date":"2021-03-08T00:18:47","date_gmt":"2021-03-08T00:18:47","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=99"},"modified":"2021-06-27T05:05:12","modified_gmt":"2021-06-27T05:05:12","slug":"php-compare-objects","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/php-compare-objects\/","title":{"rendered":"PHP Compare Objects"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to compare objects in PHP using the comparison operator ( <code>==<\/code>) and identity operator (<code>===<\/code>).<\/p>\n\n\n\n<p>To compare objects in PHP, you can use either the comparison operator (==) or identity operator (===). However, each operator behaves differently based on two main criteria:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Objects are the same instance or different instances of a class<\/li><li>Object&#8217;s properties and their values.<\/li><\/ul>\n\n\n\n<p>It&#8217;s easier to understand how the comparison operator and identity operator work via an example.<\/p>\n\n\n\n<p>Let&#8217;s create a new <a href=\"https:\/\/zentut.com\/\/php-tutorial\/php-objects-and-classes\/\">class <\/a>called <code>Point<\/code> that has two properties x coordinate and y coordinate for the demonstration.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-comment\">\/\/ Point.php<\/span>\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Point<\/span>\n<\/span>{\n\t<span class=\"hljs-keyword\">private<\/span> $x;\n\n\t<span class=\"hljs-keyword\">private<\/span> $y;\n\n\t<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">($x, $y)<\/span>\n\t<\/span>{\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;x = $x;\n\t\t<span class=\"hljs-keyword\">$this<\/span>-&gt;y = $y;\n\t}\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='comparing-objects-using-the-comparison-operator'>Comparing objects using the comparison operator (==) <a href=\"#comparing-objects-using-the-comparison-operator\" class=\"anchor\" id=\"comparing-objects-using-the-comparison-operator\" title=\"Anchor for Comparing objects using the comparison operator (==)\">#<\/a><\/h2>\n\n\n\n<p>When you compare objects using the comparison operator (<code>==<\/code>), two objects are equal if they are instances of the same class and have the same properties and values.<\/p>\n\n\n\n<p>First, create two new <code>Point<\/code> objects with the same properties&#8217; values and compare them:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'Point.php'<\/span>;\n\n$p1 = <span class=\"hljs-keyword\">new<\/span> Point(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);\n$p2 = <span class=\"hljs-keyword\">new<\/span> Point(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);\n\n<span class=\"hljs-keyword\">if<\/span> ($p1 == $p2) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p2 are equal.'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p2 are not equal.'<\/span>;\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It returns the following message:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">p1 and p2 are equal<\/code><\/span><\/pre>\n\n\n<p>Second, assign&nbsp; <code>$p2<\/code> to a new reference <code>$p3<\/code>. In this case, both&nbsp; <code>$p2<\/code> and <code>$p3<\/code> are referencing the same object.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'Point.php'<\/span>;\n\n$p1 = <span class=\"hljs-keyword\">new<\/span> Point(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);\n$p2 = <span class=\"hljs-keyword\">new<\/span> Point(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);\n\n<span class=\"hljs-keyword\">if<\/span> ($p1 == $p2) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p2 are equal.'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p2 are not equal.'<\/span>;\n}\n\n$p3 = $p2;\n<span class=\"hljs-keyword\">if<\/span> ($p2 == $p3) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p2 and p3 are equal.'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p2 and p3 are not equal.'<\/span>;\n}\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, <code>$p2<\/code>&nbsp;and&nbsp;<code>$p3<\/code> are also equal.<\/p>\n\n\n\n<p>Third, create a new <code>Point<\/code> object with different properties&#8217; values and compare it with <code>$p3<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$p4 = <span class=\"hljs-keyword\">new<\/span> Point(<span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">10<\/span>);\n<span class=\"hljs-keyword\">if<\/span> ($p3 == $p4) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p3 and p4 are equal.'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p3 and p4 are not equal.'<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, <code>$p3<\/code>&nbsp;and&nbsp; <code>$p4<\/code> are not equal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='comparing-objects-using-identity-operator'>Comparing objects using identity operator (===) <a href=\"#comparing-objects-using-identity-operator\" class=\"anchor\" id=\"comparing-objects-using-identity-operator\" title=\"Anchor for Comparing objects using identity operator (===)\">#<\/a><\/h2>\n\n\n\n<p>When you use the identity operator (<code>===<\/code>) to compare objects, they are identical if and only if both of them reference the same instance of a class.<\/p>\n\n\n\n<p>The following&nbsp; <code>$p1<\/code> and&nbsp; <code>$p2<\/code> objects are identical when we use identity operator ( <code>===<\/code>) to compare because they reference the same object.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'Point.php'<\/span>;\n\n$p1 = <span class=\"hljs-keyword\">new<\/span> Point(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);\n$p2 = $p1;\n\n<span class=\"hljs-keyword\">if<\/span> ($p1 === $p2) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p2 are identical.'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p2 are not identical.'<\/span>;\n}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However the following&nbsp; <code>$p3<\/code> object is not identical to the&nbsp; <code>$p1<\/code> even their properties values are the equal.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'Point.php'<\/span>;\n\n$p1 = <span class=\"hljs-keyword\">new<\/span> Point(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);\n$p2 = $p1;\n\n<span class=\"hljs-keyword\">if<\/span> ($p1 === $p2) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p2 are identical.'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p2 are not identical.'<\/span>;\n}\n\n$p3 = <span class=\"hljs-keyword\">new<\/span> Point(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>);\n<span class=\"hljs-keyword\">if<\/span> ($p1 === $p3) {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p3 are identical.'<\/span>;\n} <span class=\"hljs-keyword\">else<\/span> {\n\t<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">'p1 and p3 are not identical.'<\/span>;\n}\n<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following table illustrates the differences between == and === operators when comparing two objects:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Criteria<\/th><th class=\"has-text-align-center\" data-align=\"center\">==<\/th><th class=\"has-text-align-center\" data-align=\"center\">===<\/th><\/tr><\/thead><tbody><tr><td>Two objects reference the same instance<\/td><td class=\"has-text-align-center\" data-align=\"center\">true<\/td><td class=\"has-text-align-center\" data-align=\"center\">true<\/td><\/tr><tr><td>Objects with matching properties<\/td><td class=\"has-text-align-center\" data-align=\"center\">true<\/td><td class=\"has-text-align-center\" data-align=\"center\">false<\/td><\/tr><tr><td>Objects with different properties<\/td><td class=\"has-text-align-center\" data-align=\"center\">false<\/td><td class=\"has-text-align-center\" data-align=\"center\">false<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>The comparison operator (<code>==<\/code>) returns <code>true<\/code> if two objects are the same or different instances of a class with the same properties&#8217; values.<\/li><li>The identity operator (===) returns <code>true<\/code> if two objects reference the same instance of a class.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"99\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-compare-objects\/\"\n\t\t\t\tdata-post-title=\"PHP Compare Objects\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"99\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/php-compare-objects\/\"\n\t\t\t\tdata-post-title=\"PHP Compare Objects\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to compare objects in PHP using comparison operator ( ==) and identity operator ( ===).<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1753,"menu_order":26,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-99","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/99\/revisions"}],"predecessor-version":[{"id":2047,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/99\/revisions\/2047"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}