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

XMLWriter::writeAttribute

PHP function Edit on GitHub ✎

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

Write full attribute

Description

Oop

XMLWriter::writeAttribute(string $name, string $value): bool

Procedural

xmlwriter_write_attribute(XMLWriter $writer, string $name, string $value): bool

Writes a full attribute.

Parameters

name

The name of the attribute.

value

The value of the attribute.

Return Values

Success

Changelog

VersionDescription

Examples

Intermixing Sub-elements and Attributes

If writing sub-elements and attributes is intermixed, any attempt to write attributes after the first sub-element will fail and return false.

php
<?php
$xml = new XMLWriter();
$xml->openMemory();

$xml->startElement('element');
$xml->writeAttribute('attr1', '0');
$xml->writeElement('subelem', '0');
var_dump($xml->writeAttribute('attr2', '0'));
$xml->endElement();

echo $xml->flush();
?>

The above example will output:

output
bool(false)
<element attr1="0"><subelem>0</subelem></element>

See Also

  • XMLWriter::writeAttributeNs
  • XMLWriter::startAttribute
  • XMLWriter::startAttributeNs
  • XMLWriter::endAttribute

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