PHP | SimpleXMLElement addAttribute() Function
Pre-requisite: Read XML Basics
The SimpleXMLElement::addAttribute() function is an inbuilt function in PHP which add an attribute in a SimpleXML object.
Syntax:
void SimpleXMLElement::addAttribute($name, $value, $namespace)
Parameter: This function accepts three parameters as mentioned above and described below:
- $name: It is required parameter. It specifies the name of the attribute to be added.
- $value: It is optional parameter. It specifies the value of the attribute to be added.
- $namespace: It is optional parameter. It specifies namespace for the attribute.
Return Value: This function does not accepts any parameters.
Note: This function is available for PHP 5.1.3 and newer version.
Example:
<?php // Loading XML document to $user $user = <<<XML <user> <username> user123 </username> <name> firstname lastname </name> <phone> +91-9876543210 </phone> <detail> I am John Doe. Live in Kolkata, India. </detail> </user> XML; // Creating new SimpleXMLElement // object from $user $xml = new SimpleXMLElement($user); // Adding child named "institution" // and valued "geeksforgeeks" $xml->addChild("institution", "geeksforgeeks"); // Adding attribute named "type" and value // "educational" in institution element. $xml->institution->addAttribute("type", "educational"); // Printing as XML echo $xml->asXML(); echo $xml->asXML('savexmltofile.xml'); ?> |
Output:
user123 firstname lastname +91-9876543210 I am John Doe. Live in Kolkata, India. geeksforgeeks 1
Source code in browser:
<?xml version="1.0"?> <user> <username> user123 </username> <name> firstname lastname </name> <phone> +91-9876543210 </phone> <detail> I am John Doe. Live in Kolkata, India. </detail> <institution type="educational">geeksforgeeks</institution></user> <br>1 |
Reference: https://www.php.net/manual/en/simplexmlelement.addattribute.php
Recommended Posts:
- PHP | SimpleXMLElement attributes() Function
- PHP | SimpleXMLElement saveXML() Function
- PHP | SimpleXMLElement addChild() Function
- PHP | SimpleXMLElement asXML() Function
- PHP | SimpleXMLElement count() Function
- PHP | SimpleXMLElement getNamespaces() Function
- PHP | SimpleXMLElement getDocNamespaces() Function
- PHP | SimpleXMLElement registerXPathNamespace() Function
- PHP | SimpleXMLElement::__construct() Function
- PHP | SimpleXMLElement children() Function
- PHP | SimpleXMLElement XPath() Function
- PHP | SimpleXMLElement::getName() Function
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


