PHP | DOMDocument createAttribute() Function Last Updated : 27 Aug, 2019 Comments Improve Suggest changes Like Article Like Report The DOMDocument::createAttribute() function is an inbuilt function in PHP which is used to create a new instance of class DOMAttr. Syntax: DOMAttr DOMDocument::createAttribute( string $name ) Parameters: This function accepts single parameter $name which holds the name of the attribute. Return Value: This function returns the new DOMAttr object on success or FALSE on failure. Below programs illustrate the DOMDocument::createAttribute() function in PHP: Program 1: php <?php // Create a new DOMDocument $domDocument = new DOMDocument('1.0', 'iso-8859-1'); // Use createElement() function to create an element node $domElement = $domDocument->createElement('organization', 'A computer science portal'); // Use createAttribute() function to create an attribute node $domAttribute = $domDocument->createAttribute('name'); // Value of created attribute $domAttribute->value = 'GeeksforGeeks'; // Append element to the document $domElement->appendChild($domAttribute); // Append it to the document itself $domDocument->appendChild($domElement); // Save the document into XML and display it echo $domDocument->saveXML(); ?> Output: <?xml version="1.0" encoding="iso-8859-1"?> <organization name="GeeksforGeeks">A computer science portal</organization> Program 2: php <?php // Create a new DOMDocument $domDocument = new DOMDocument('1.0', 'iso-8859-1'); // Use createElement() function to create an element node $domElement = $domDocument->createElement('organization', 'GeeksforGeeks'); $domAttribute1 = $domDocument->createAttribute('name'); // Value for the created attribute $domAttribute1->value = 'GeeksforGeeks'; // Append element to the document $domElement->appendChild($domAttribute1); // Use createAttribute() function to create an attribute node $domAttribute2 = $domDocument->createAttribute('address'); // Value for the created attribute $domAttribute2->value = 'Noida'; // Append element to the document $domElement->appendChild($domAttribute2); // Use createAttribute() function to create an attribute node $domAttribute3 = $domDocument->createAttribute('email'); // Value for the created attribute $domAttribute3->value = '[email protected]'; // Append element to the document $domElement->appendChild($domAttribute3); // Append it to the document itself $domDocument->appendChild($domElement); // Save the document to XML and display it echo $domDocument->saveXML(); ?> Output: <?xml version="1.0" encoding="iso-8859-1"?> <organization name="GeeksforGeeks" address="Noida" email="[email protected]"> GeeksforGeeks </organization> Reference: https://www.php.net/manual/en/domdocument.createattribute.php Comment More infoAdvertise with us Next Article PHP | DOMDocument createAttribute() Function J jit_t Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP | DOMDocument createAttributeNS() Function The DOMDocument::createAttributeNS() function is an inbuilt function in PHP which is used to create a new attribute node with an associated namespace. Syntax: DOMAttr DOMDocument::createAttributeNS( string $namespaceURI, string $qualifiedName ) Parameters: This function accepts two parameters as men 2 min read PHP | DOMDocument createTextNode() Function The DOMDocument::createTextNode() function is an inbuilt function in PHP which is used to create a new instance of class DOMText. Syntax: DOMText DOMDocument::createTextNode( string $content ) Parameters: This function accepts single parameter $content which holds the content of the text. Return Val 1 min read PHP | DOMDocument createCDATASection() Function The DOMDocument::createCDATASection() function is an inbuilt function in PHP which is used to create a new instance of class DOMCDATASection. Syntax: DOMCDATASection DOMDocument::createCDATASection( string $data ) Parameters: This function accepts single parameter $data which holds the content of th 1 min read PHP | DOMDocument createElement() Function The DOMDocument::createElement() function is an inbuilt function in PHP which is used to create a new instance of class DOMElement. Syntax: DOMElement DOMDocument::createElement( string $name, string $value ) Parameters: This function accepts two parameters as mentioned above and described below: $n 2 min read PHP | DOMDocument createComment() Function The DOMDocument::createComment() function is an inbuilt function in PHP which is used to create a new instance of class createComment. Syntax: DOMComment DOMDocument::createComment( string $data ) Parameters: This function accepts single parameter $data which holds the content of the comment node. R 2 min read Like