If you intermix writing sub-elements and attributes, any attempt to write attributes after the first sub-element will fail and return false:
<?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();
?>
Outputs:
bool(false)
<element attr1="0"><subelem>0</subelem></element>
This is because this is a forward-only writer and the start tags are already finished. There is no going back to add more attributes later, there is no place to put attr2. This is exactly how it is supposed to work.