PHP SimpleXMLElement::getName() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::getName() returns the name of an XML element or attribute.

It is helpful inside a loop, when you want to know which element you are looking at. The name does not include any namespace prefix.

Syntax

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

PHP
$name = $book->getName();
CallWhat it does
$book->getName()Returns the name of the root element, here book.
$book->title->getName()Returns the name of a child element, here title.
$book->title['id']->getName()Returns the name of an attribute, here id.

For a namespaced element such as <x:item>, getName() returns only the local name item, without the prefix.

Library declarations
PHP
SimpleXMLElement::getName(): string

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
stringThe name of the element or attribute.

PHP Examples

Example 1 — Get Element Names

Print the name of the root element and of its child.

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

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

How It Works

  1. simplexml_load_string() returns the root element, book.
  2. $book->getName() returns that name.
  3. $book->title selects the child element.
  4. getName() on the child returns title.

Example 2 — Get an Attribute Name

An attribute is also a SimpleXMLElement, so it has a name too.

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

$attribute = $book->title['id'];

echo $attribute->getName(), PHP_EOL;
echo (string) $attribute, PHP_EOL;

How It Works

  1. $book->title['id'] selects the id attribute of the title element.
  2. getName() returns the attribute name, id.
  3. Casting the same object to a string returns its value.
  4. So one object gives you both the name and the value.

Example 3 — Name Every Child in a Loop

Loop over the children and print each element name with its text.

PHP
<?php
$book = simplexml_load_string('<book><title>PHP</title><author>Ann</author></book>');

foreach ($book->children() as $child) {
    echo $child->getName(), " = ", (string) $child, PHP_EOL;
}

How It Works

  1. children() returns the two child elements of book.
  2. foreach visits them one by one.
  3. getName() gives the element name for the current child.
  4. The cast gives the text, so each line shows a name and a value.

Frequently Asked Questions

It returns the name of the element or attribute as a string. For <book> it returns book.
No. It returns only the local name. For <x:item> you get item.
Yes. An attribute is also a SimpleXMLElement, so $book->title['id']->getName() returns id.
Call getName() for the name and cast the element with (string) for the text.
Yes. When you loop with foreach ($book as $name => $child), the key is the element name.

Next: SimpleXMLElement::getNamespaces()

SimpleXMLElement::getNamespaces() returns the namespaces that are used by an element and i.

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