PHP SimpleXMLElement::__toString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::__toString() returns the text content of an element as a plain string.

PHP calls this method for you when you cast an element with (string). You can also call it by hand. It returns only the text that sits directly inside the element, not the text of its child elements.

Syntax

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

PHP
$text = (string) $book->title;
CallWhat it does
(string) $book->titleThe usual way to get the text of an element. PHP calls __toString() behind the scenes.
$book->title->__toString()Calls the method directly and returns the same text.
(string) $book->title['id']Casts an attribute to a string to read its value.

Always cast to a string when you compare or store the value. A SimpleXMLElement is an object, so $book->title is not the same thing as the text inside it.

Library declarations
PHP
SimpleXMLElement::__toString(): string

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
stringThe text content of the element. It is an empty string when the element has only child elements.

PHP Examples

Example 1 — Cast an Element to a String

Read the title text with a cast and with a direct method call. Both give the same result.

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

echo (string) $book->title, PHP_EOL;
echo $book->title->__toString(), PHP_EOL;

How It Works

  1. simplexml_load_string() parses the XML and returns the root element.
  2. $book->title is a SimpleXMLElement object, not a plain string.
  3. The (string) cast makes PHP call __toString() for you.
  4. Calling __toString() by hand returns exactly the same text.

Example 2 — Elements with Only Children Give an Empty String

The text of the root element is empty because its content is made of child elements, not text.

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

var_dump((string) $book);
var_dump((string) $book->title);

How It Works

  1. The book element contains only the title element and no text of its own.
  2. (string) $book therefore returns an empty string, shown by string(0).
  3. The title element holds the text PHP.
  4. (string) $book->title returns it as a string of 3 characters.

Example 3 — Convert Text to Numbers and Attributes

XML values are always text. Cast an attribute and a price to the type you need.

PHP
<?php
$book = simplexml_load_string('<book id="7"><price>9.50</price></book>');

$id = (int) $book['id'];
$price = (float) (string) $book->price;

echo "ID: ", $id, PHP_EOL;
echo "Price plus one: ", $price + 1, PHP_EOL;

How It Works

  1. The book element has an id attribute and a price child element.
  2. (int) $book['id'] turns the attribute value 7 into an integer.
  3. (float) (string) $book->price gets the text first and then converts it to a float.
  4. Because both values are numbers now, the math works and prints 10. 5.

Frequently Asked Questions

It returns the text that sits directly inside the element as a string. For an element with only child elements, it returns an empty string.
Usually not. Casting with (string) calls it for you, and echo does the same. Calling it directly gives the same result.
$book->title is an object, and a strict comparison with a string is never true. Compare (string) $book->title with 'PHP' instead.
No. It returns only the text directly inside the element. To read a child, cast that child on its own, for example (string) $book->title.
Cast the attribute the same way, like (string) $book->title['id']. The attribute is also a SimpleXMLElement, so the same rule applies.

Next: SimpleXMLElement::addAttribute()

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

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