PHP SimpleXMLElement::addAttribute() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::addAttribute() adds a new attribute with a value to an XML element.

Use it when you build or change XML in code. You call it on the element that should get the attribute, and it changes the object in place. It does not return anything.

Syntax

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

PHP
$book->title->addAttribute('lang', 'en');
CallWhat it does
$book->title->addAttribute('lang', 'en')Adds lang="en" to the title element.
$book->addAttribute('isbn', '12345')Adds an attribute to the root element.
$book->title->addAttribute('xml:lang', 'en', 'http://www.w3.org/XML/1998/namespace')Adds a namespaced attribute by passing the namespace URI.

Adding an attribute that already exists raises a warning, so check with isset() first. Attribute values are always stored as text.

Library declarations
PHP
SimpleXMLElement::addAttribute(string $qualifiedName, string $value, ?string $namespace = null): void

Parameters

ParameterDescription
qualifiedNameRequired. The attribute name. It can include a namespace prefix such as xml:lang.
valueRequired. The attribute value as a string.
namespaceOptional. The namespace URI for the attribute. The default is null.

Return Value

Return ValueDescription
voidNothing is returned. The element is changed directly.

PHP Examples

Example 1 — Add One Attribute

Add a lang attribute to the title element, then print the element as XML.

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

$book->title->addAttribute('lang', 'en');

echo $book->title->asXML(), PHP_EOL;
echo (string) $book->title['lang'], PHP_EOL;

How It Works

  1. The title element starts with only an id attribute.
  2. addAttribute('lang', 'en') adds a second attribute to the same element.
  3. asXML() prints the updated element with both attributes.
  4. $book->title['lang'] reads the new value, cast to a string.

Example 2 — Skip Attributes That Already Exist

Check with isset() before adding, so you never add the same attribute twice.

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

$attributes = ['id' => '2', 'lang' => 'en'];

foreach ($attributes as $name => $value) {
    if (isset($book->title[$name])) {
        echo "Skipped ", $name, PHP_EOL;
    } else {
        $book->title->addAttribute($name, $value);
        echo "Added ", $name, PHP_EOL;
    }
}

echo $book->title->asXML(), PHP_EOL;

How It Works

  1. The array lists the attributes we would like to have.
  2. isset($book->title[$name]) is true for id, because it is already there.
  3. The id is skipped, and lang is added with addAttribute().
  4. The final XML keeps the original id value of 1.

Example 3 — Number Every Item in a List

Loop over the child elements and add a position attribute to each one.

PHP
<?php
$list = simplexml_load_string('<list><item>Tea</item><item>Milk</item></list>');

$position = 1;
foreach ($list->item as $item) {
    $item->addAttribute('position', (string) $position);
    $position++;
}

foreach ($list->item as $item) {
    echo $item->asXML(), PHP_EOL;
}

How It Works

  1. The list holds two item elements.
  2. The first loop adds a position attribute to each item.
  3. The number is cast to a string, because addAttribute() expects a string value.
  4. The second loop prints each updated item as XML.

Frequently Asked Questions

It adds a new attribute with a name and a value to the element you call it on. The change is made directly on the object.
No, it returns void. To check the result, read the attribute back with $element['name'] or print the element with asXML().
PHP raises a warning and the old value stays. Check with isset($element['name']) first, or assign a new value with $element['name'] = 'value'.
Yes. Pass the prefix in the name, like xml:lang, and the namespace URI as the third argument.
Call asXML() with a filename after adding the attribute. The method writes the whole document to that file.

Next: SimpleXMLElement::addChild()

SimpleXMLElement::addChild() adds a new child element to an XML element and returns that n.

SimpleXMLElement::addChild() 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