PHP SimpleXMLElement::attributes() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::attributes() returns the attributes of an element, so you can loop over their names and values.

The result is a SimpleXMLElement that holds the attributes. Use foreach to read each name and value, or use array style like $attrs['id'] to read one. By default it returns the attributes without a namespace.

Syntax

Call SimpleXMLElement::attributes() with the values you need. Start with this line.

PHP
$attrs = $book->title->attributes();
CallWhat it does
$book->title->attributes()Returns all attributes of the title element.
$book->title->attributes()['id']Reads one attribute value straight from the result.
$book->attributes('x', true)Returns the attributes in the namespace with the prefix x.

You can read a single attribute faster with $book->title['id']. Use attributes() when you want to loop over all of them or work with namespaces.

Library declarations
PHP
SimpleXMLElement::attributes(?string $namespaceOrPrefix = null, bool $isPrefix = false): ?SimpleXMLElement

Parameters

ParameterDescription
namespaceOrPrefixOptional. A namespace URI or prefix. The default is null, which means attributes without a namespace.
isPrefixOptional. Set to true when the first argument is a prefix and not a URI. The default is false.

Return Value

Return ValueDescription
SimpleXMLElementAn object that holds the attributes. It is empty when the element has no attributes.
nullReturned when called on an attribute node.

PHP Examples

Example 1 — Loop Over Attributes

Read every attribute of the book element with foreach.

PHP
<?php
$book = simplexml_load_string('<book id="7" lang="en"><title>PHP</title></book>');

foreach ($book->attributes() as $name => $value) {
    echo $name, "=", (string) $value, PHP_EOL;
}

How It Works

  1. The book element has two attributes, id and lang.
  2. attributes() returns an object that holds both of them.
  3. foreach gives the attribute name as the key and the value as an object.
  4. The value is cast to a string before it is printed.

Example 2 — Read One Value and Count Attributes

Use array style to read one attribute, and count() to see how many an element has.

PHP
<?php
$book = simplexml_load_string('<book><title id="1">PHP</title></book>');

$attrs = $book->title->attributes();

echo (string) $attrs['id'], PHP_EOL;
echo count($attrs), PHP_EOL;
echo count($book->attributes()), PHP_EOL;

How It Works

  1. $book->title->attributes() returns the attributes of the title element.
  2. $attrs['id'] reads one attribute, cast to a string.
  3. count($attrs) returns 1, because title has one attribute.
  4. The root book has no attributes, so its count is 0.

Example 3 — Build an Array of Attributes

Turn the attributes of an element into a normal PHP array.

PHP
<?php
$book = simplexml_load_string('<book id="7" lang="en"><title>PHP</title></book>');

$data = [];
foreach ($book->attributes() as $name => $value) {
    $data[$name] = (string) $value;
}

echo json_encode($data), PHP_EOL;

How It Works

  1. An empty array is prepared to hold the results.
  2. foreach goes through each attribute of the book element.
  3. Each value is cast to a string and stored under the attribute name.
  4. json_encode() prints the array in a short, readable form.

Frequently Asked Questions

It returns a SimpleXMLElement object that holds the attributes of the element. You can loop over it or read single attributes by name.
Use array style, like $book->title['id'], or $book->title->attributes()['id']. Cast the result to a string to get the text.
Loop with foreach and store each value, cast to a string, under its name. You can then use json_encode() or print_r() on the array.
Pass the namespace URI, or pass a prefix and set the second argument to true, like attributes('x', true).
It returns null only when it is called on an attribute node. For a normal element it returns an object, which is empty if there are no attributes.

Next: SimpleXMLElement::children()

SimpleXMLElement::children() returns the child elements of an element, so you can loop ove.

SimpleXMLElement::children() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful