PHP attributes() Function
In this chapter you will learn:
- Description for PHP attributes() Function
- Syntax for PHP attributes() Function
- Parameter for PHP attributes() Function
- Return for PHP attributes() Function
- Example - Return attributes and values within the XML body element
Description
The attributes() function returns attributes and values within an XML tag.
Syntax
PHP attributes() Function has the following syntax.
attributes(ns,is_prefix);
Parameter
Parameter | Is Required | Description |
---|---|---|
ns | Optional. | A namespace for the retrieved attributes |
is_prefix | Optional. | A Boolean value. TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE |
Return
Returns a SimpleXMLElement object on success.
Example
Return attributes and values within the XML body element:
<?php/*from java 2 s. com*/
$note=<<<XML
<book>
<name date="2013-01-01" type="public">PHP</name>
<name date="2013-01-01" type="private">Java</name>
</book>
XML;
$xml=simplexml_load_string($note);
foreach($xml->body[0]->attributes() as $a => $b){
echo $a,'="',$b,"\"\n";
}
?>
Next chapter...
What you will learn in the next chapter:
- Description for PHP children() Function
- Syntax for PHP children() Function
- Parameter for PHP children() Function
- Return for PHP children() Function
- Example - Find the children of the note node
Home » PHP Tutorial » PHP SimpleXML Functions