(PHP 8 >= 8.3.0)
DOMElement::toggleAttribute — Toggle attribute
public DOMElement::toggleAttribute(string $qualifiedName, ?bool $force = null): bool
Toggle the attribute.
Example #1 DOMElement::toggleAttribute() example
<?php
$dom = new DOMDocument();
$dom->loadXML("<?xml version='1.0'?><container selected=\"\"/>");
var_dump($dom->documentElement->toggleAttribute('selected'));
echo $dom->saveXML() . PHP_EOL;
var_dump($dom->documentElement->toggleAttribute('selected'));
echo $dom->saveXML();
?> The above example will output:
bool(false) <?xml version="1.0"?> <container/> bool(true) <?xml version="1.0"?> <container selected=""/>
© 1997–2025 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/domelement.toggleattribute.php