PHP | DOMAttr __construct() Function Last Updated : 25 Sep, 2019 Comments Improve Suggest changes Like Article Like Report The DOMAttr::__construct() function is an inbuilt function in PHP which is used to create a new DOMAttr object. This created object is a read-only type. Syntax: public DOMAttr::__construct( string $name, string $value ) Parameters: This function accepts two parameters as mentioned above and described below: $name: This parameter holds the element name of the attribute. $value: This parameter holds the value of the attribute. Below programs illustrate the DOMAttr::__construct() function in PHP: Program 1: php <?php // Create a new DOMDocument object $domDocument = new DOMDocument('1.0', 'iso-8859-1'); // Create a root element $rootElement = new DOMElement('root'); // Append the element as child element $element = $domDocument->appendChild($rootElement); // Create an attribute $domAttr = new DOMAttr('attr', 'GeeksforGeeks'); // Set the attribute to the node $attr = $element->setAttributeNode($domAttr); // Display the XML document echo $domDocument->saveXML(); ?> Output: <?xml version="1.0" encoding="iso-8859-1"?> <root attr="GeeksforGeeks"/> Program 2: php <?php // Create a new DOMDocument object $domDocument = new DOMDocument('1.0', 'iso-8859-1'); // Create a root element $rootElement = new DOMElement('root'); // Append the element as child element $element = $domDocument->appendChild($rootElement); // Create an attribute $domAttr1 = new DOMAttr('Name', 'GeeksforGeeks'); // Set the attribute to the node $attr = $element->setAttributeNode($domAttr1); // Create an attribute $domAttr2 = new DOMAttr('Address', 'Noida'); // Set the attribute to the node $attr = $element->setAttributeNode($domAttr2); // Create an attribute $domAttr3 = new DOMAttr('mail', '[email protected]'); // Set the attribute to the node $attr = $element->setAttributeNode($domAttr3); // Display the XML document echo $domDocument->saveXML(); ?> Output: <?xml version="1.0" encoding="iso-8859-1"?> <root Name="GeeksforGeeks" Address="Noida" mail="[email protected]"/> Reference: https://www.php.net/manual/en/domattr.construct.php Comment More infoAdvertise with us Next Article PHP | DOMAttr __construct() Function J jit_t Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMComment __construct() Function The DOMComment::__construct() function is an inbuilt function in PHP which creates a new DOMComment object. This object is read only and can be appended to a document Syntax: public DOMComment::__construct( string $value) Parameters: This function accepts a single parameter $value which holds the co 1 min read PHP | DOMElement __construct() Function The DOMElement::__construct() function is an inbuilt function in PHP which is used to create a new DOMElement object. This object is read-only and may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. Syntax: public DOMElem 2 min read PHP | DOMCdataSection __construct() Function The DOMCdataSection::__construct() function is an inbuilt function in PHP which is used to construct a new DOMCdataSection object. DOMC stands for DOM Character and this section can further be manipulated using the methods of DOMCharacterData class. This CDATA node works like the DOMTEXT class. Synt 1 min read PHP | DOMDocument __construct() Function The DOMDocument::__construct() function is an inbuilt function in PHP which is used to create a new DOMDocument object. Syntax: public DOMDocument::__construct( string $version, string $encoding ) Parameters: This function accepts two parameters as mentioned above and described below: $version: This 1 min read PHP | DOMEntityReference __construct() function The DOMEntityReference::__construct() function is an inbuilt function in PHP which is used to create a new DOMEntityReference object. Syntax: public DOMEntityReference::__construct( string $name ) Parameters:This function accepts a single parameter $name which holds the name of the entity reference. 1 min read Like