Latest web development tutorials

PHP addAttribute () function

PHP SimpleXML Reference Manual PHP SimpleXML Reference Manual

Examples

Root element and the body element to add an attribute:

<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;

$xml=new SimpleXMLElement($note);
$xml->addAttribute("type","private");
$xml->body->addAttribute("date","2013-01-01");

echo $xml->asXML();
?>

Running instance »

Definition and Usage

addAttribute () function to add an attribute to the SimpleXML element.


grammar

addAttribute( name,value,ns );

参数 描述
name 必需。规定要添加的属性的名称。
value 可选。规定属性的值。
ns 可选。规定属性的命名空间。

technical details

return value: No return value.
PHP version: 5.1.3+


PHP SimpleXML Reference Manual PHP SimpleXML Reference Manual