php8.5
Home/ Manual/ simplexml / simplexmlelement/ SimpleXMLElement::attributes

SimpleXMLElement::attributes

PHP function Edit on GitHub ✎

(PHP 5, PHP 7, PHP 8)

Identifies an element's attributes

Description

SimpleXMLElement::attributes(string|null $namespaceOrPrefix = null, bool $isPrefix = false): SimpleXMLElement|null

This function provides the attributes and values defined within an xml tag.

Iteration

Parameters

namespaceOrPrefix

An optional namespace for the retrieved attributes

isPrefix

Default to false

Return Values

Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.

Returns null if called on a SimpleXMLElement object that already represents an attribute and not a tag.

Examples

Interpret an XML string

php
<?php
$string = <<<XML
<a>
 <foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}
?>

The above example will output:

output
name="one"
game="lonely"

See Also

Source: reference/simplexml/simplexmlelement/attributes.xml · from the official PHP manual (php/doc-en)