Monday, December 22, 2025
HomeLanguagesPHP | DOMDocument createAttribute() Function

PHP | DOMDocument createAttribute() Function

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
  
// 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 = 'neveropen';
  
// 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="neveropen">A computer science portal</organization>

Program 2:




<?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',
                                         'neveropen');
  
$domAttribute1 = $domDocument->createAttribute('name');
  
// Value for the created attribute
$domAttribute1->value = 'neveropen';
  
// 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 = 'abc@geeksforgeeks.org';
  
// 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="neveropen" address="Noida" email="abc@geeksforgeeks.org">
    neveropen
</organization>

Reference: https://www.php.net/manual/en/domdocument.createattribute.php

RELATED ARTICLES

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS