略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DOMElement::toggleAttribute

2025-02-27

DOMElement::toggleAttribute

(PHP 8 >= 8.3.0)

DOMElement::toggleAttributeToggle attribute

说明

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

Toggle the attribute.

参数

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.

返回值

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=""/>
添加备注

用户贡献的备注

此页面尚无用户贡献的备注。

官方地址:https://www.php.net/manual/en/domelement.toggleattribute.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3