note 45594 added to function.simplexml-element-attributes
| From: | EirikSletteberg at osu1 dot php dot net | Date: | Sun, 12 Sep 2004 13:21:21 +0000 |
| Subject: | note 45594 added to function.simplexml-element-attributes | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76518@lists.php.net to get a copy of this message | ||
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;
}
}
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-element-attributes.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45594
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45594&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45594&report=yes
Search -- http://master.php.net/manage/user-notes.php