note 49077 added to types.comparisons

From: Date: Sun, 16 Jan 2005 05:58:16 +0000
Subject: note 49077 added to types.comparisons
Groups: php.notes 
Request: Send a blank email to php-notes+get-83396@lists.php.net to get a copy of this message
I didn't see any encompassing information regarding the effects of the '===' operator when dealing with object & array() comparisons. Here are some pertinent facts worth understanding. NOTE: When dealing with objects It would seem that this operator will test the provided object's class type, as well as its member variables. This example: <? class MyClassA { var $bMember = NULL; function MyClassA($bMember) {$this->bMember = $bMember;} } $objA = new MyClassA(true); $objB = new MyClassA(false); echo "These two classes are: ".(($objA === $objB) ? "equal!" : "not equal!"); ?> produces the output: These two classes are: not equal! NOTE: When dealing with arrays: Identical comparisons ( '===') largely work as you'd expect, only returning true if the contents of the arrays match exactly. The only inconsistency (depending on how you want to lookat it) is that you might get caught by however is the following example: <? $a_A = array(); $a_A['A'] = 1; $a_A['B'] = 2; $a_B = array(); $a_B['B'] = 2; $a_B['A'] = 1; echo "These two arrays are: ".(($a_B === $a_B) ? "equal!" : "not equal!"); ?> which produces the output: These two arrays are: equal! The reason this is perhaps inconsistent is because $a_A 's 'internal pointer' is set to array('A' => 1) , whereas $a_B's 'internal pointer' is set to array('B' => 2) IMHO, I think this was the most appropriate way of handling the scenario. ---- Manual Page -- http://www.php.net/manual/en/types.comparisons.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49077 Delete -- http://master.php.net/manage/user-notes.php?action=delete+49077&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49077&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+49077&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+49077&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+49077&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+49077&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+49077&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#83396) next »