note 40044 deleted from function.simplexml-load-string by cmb
| From: | cmb@php.net | Date: | Sun, 15 Dec 2019 10:13:21 +0000 |
| Subject: | note 40044 deleted from function.simplexml-load-string by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213531@lists.php.net to get a copy of this message | ||
Note Submitter: greg dot steffensen at spamless dot richmond dot edu
----
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:
<?php
$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.]