php8.5
Home/ Manual/ dom / domelement/ DOMElement::toggleAttribute

DOMElement::toggleAttribute

PHP function Edit on GitHub ✎

(PHP 8 >= 8.3.0)

Toggle attribute

Description

DOMElement::toggleAttribute(string $qualifiedName, bool|null $force = null): bool

Toggle the attribute.

Parameters

qualifiedName

The qualified name of the attribute.

force
  • if null, the function will toggle the attribute.
  • if true, the function adds the attribute.
  • if false, the function removes the attribute.

Return Values

Returns true if the attribute is present after finishing the call, false otherwise.

Examples

DOMElement::toggleAttribute example

php
<?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:

output
bool(false)
<?xml version="1.0"?>
<container/>

bool(true)
<?xml version="1.0"?>
<container selected=""/>

Source: reference/dom/domelement/toggleattribute.xml · from the official PHP manual (php/doc-en)