(PHP 8 >= 8.3.0)
DOMElement::toggleAttribute — Toggle attribute
$qualifiedName, ?bool $force = null): boolToggle the attribute.
qualifiedNameThe qualified name of the attribute.
force
null, the function will toggle the attribute.true, the function adds the attribute.false, the function removes the attribute.
Returns true if the attribute is present after finishing the call, false otherwise.
示例 #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();
?>以上示例会输出:
bool(false) <?xml version="1.0"?> <container/> bool(true) <?xml version="1.0"?> <container selected=""/>