php8.5
Home/ Manual/ xmlwriter / xmlwriter/ XMLWriter::setIndent

XMLWriter::setIndent

PHP function Edit on GitHub ✎

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL xmlwriter >= 0.1.0)

Toggle indentation on/off

Description

Oop

XMLWriter::setIndent(bool $enable): bool

Procedural

xmlwriter_set_indent(XMLWriter $writer, bool $enable): bool

Toggles indentation on or off.

Parameters

enable

Whether indentation is enabled.

Return Values

Success

Changelog

VersionDescription

Examples

XMLWriter::setIndent and mixed Content

Enabling indentation is not suitable for mixed content, because the indent string is also inserted before inline elements.

php
<?php
$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent(true);
$writer->startDocument();
$writer->startElement('p');
$writer->text('before');
$writer->writeElement('a', 'element');
$writer->text('after');
$writer->endElement();
$writer->endDocument();
echo $writer->outputMemory();
?>

The above example will output:

output
<?xml version="1.0"?>
<p>before <a>element</a>
after</p>

Notes

Note

The indent is reset when an xmlwriter is opened.

See Also

  • XMLWriter::setIndentString

Source: reference/xmlwriter/xmlwriter/setindent.xml · from the official PHP manual (php/doc-en)