note 40044 modified in ref.simplexml by kennyt

From: Date: Sun, 18 Jul 2004 17:01:23 +0000
Subject: note 40044 modified in ref.simplexml by kennyt
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-73494@lists.php.net to get a copy of this message
Simplexml's simplicity can be deceptive. Simplexml elements behave either as objects or strings, depending on the context in which they're used (through overloading of the __toString() method, I assume). Statements implying conversion to string treat them as strings, while assignment operations treat them as objects. This can lead to unexpected behavior if, for example, you are trying to compare the values of two Simplexml elements. The expected syntax will not work. To force conversion to strings, just "typecast' whatever Simplexml element you're using. For example: $s = simplexml_load_string('<foo>43</foo> <bar>43</bar>'); // Evaluates to false by comparing object IDs instead of strings ($s->foo == $s->bar); // Evaluates to true ((string)$s->foo == (string)$s->bar); [Ed. Note: Changed from quotes to casts because casts provide a quicker and more explicit conversion than do double quotes.] --was-- Simplexml's simplicity can be deceptive. Simplexml elements behave either as objects or strings, depending on the context in which they're used (through overloading of the __toString() method, I assume). Statements implying conversion to string treat them as strings, while assignment operations treat them as objects. This can lead to unexpected behavior if, for example, you are trying to compare the values of two Simplexml elements. The expected syntax will not work. To force conversion to strings, just place whatever Simplexml element you're using inside double quotes. For example: $s = simplexml_load_string('<foo>43</foo> <bar>43</bar>'); // Evaluates to false ($s->foo == $s->bar); // Evaluates to true ("$s->foo" == "$s->bar"); This is accurate as of PHP 5 Beta 4. http://php.net/manual/en/ref.simplexml.php

« previous php.notes (#73494) next »