note 45594 deleted from simplexmlelement.attributes by danbrown
| From: | danbrown@php.net | Date: | Wed, 16 Sep 2009 15:24:33 +0000 |
| Subject: | note 45594 deleted from simplexmlelement.attributes by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160627@lists.php.net to get a copy of this message | ||
Note Submitter: Eirik Sletteberg
----
In reply to inge at elektronaut dot no's function:
Notice that this function returns an object, and not
a string. This is quite hard to notice at first, because
if you validate the return against a string, it will return
true:
print findAttribute($Fox, "foo") == "bar"? "Matches.":"Won't
match."; // "Matches."
But, if you attempt to use the return for indexing in an array, like:
$MyArray[findattribute($Fox, "foo")] = "bar";
You will get an illegal offset type-error.
------
To correct this, we force-cast the return as a string:
function findAttribute($object, $attribute) {
foreach($object->attributes() as $a => $b) {
if ($a == $attribute) {
$return = $b;
}
}
if($return) {
return (string)$return;
}
}