PHP addAttribute() Function
In this chapter you will learn:
- Description for PHP addAttribute() Function
- Syntax for PHP addAttribute() Function
- Parameter for PHP addAttribute() Function
- Return for PHP addAttribute() Function
- Example - Add an attribute to the root element and to the body element
Description
The addAttribute() function adds an attribute to the SimpleXML element.
Syntax
PHP addAttribute() Function has the following syntax.
addAttribute(name,value,ns);
Parameter
Parameter | Is Required | Description |
---|---|---|
name | Required. | Name of the attribute to add |
value | Optional. | Value of the attribute |
ns | Optional. | A namespace for the attribute |
Return
No value is returned
Example
Add an attribute to the root element and to the body element:
<?php//from j a v a2s. c om
$note=<<<XML
<book>
<name>PHP</name>
<name>Java</name>
</book>
XML;
$xml=new SimpleXMLElement($note);
$xml->addAttribute("type","private");
$xml->body->addAttribute("date","2013-01-01");
echo $xml->asXML();
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Description for PHP addChild() Function
- Syntax for PHP addChild() Function
- Parameter for PHP addChild() Function
- Return for PHP addChild() Function
- Example - adds a child element to the SimpleXML element
Home » PHP Tutorial » PHP SimpleXML Functions