PHP Function Reference

PHP SimpleXMLElement - __toString() Method



The PHP SimpleXMLElement::__toString() method returns text content that is directly in this element. It does not return the text content which is inside this element's children.

Syntax

public SimpleXMLElement::__toString()

Parameters

No parameter is required.

Return Value

Returns the string content on success or an empty string on failure.

Example: getting string content

The example below shows the usage of SimpleXMLElement::__toString() method.

<?php
$xmlstr = <<<XML
<a>1 <b>2</b> <b>3</b> 4</a> 
XML;

$xml = new SimpleXMLElement($xmlstr);

//text content of element a
echo $xml."\n";

//text content of element b
echo $xml->b[0]."\n";
echo $xml->b[1]."\n";
?>

The output of the above code will be:

1   4
2
3

❮ PHP SimpleXML Reference