Friday, October 24, 2025
HomeLanguagesPHP | DOMDocument createTextNode() Function

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 Value: This function returns a new DOMText object on success or FALSE on failure.

Below programs illustrate the DOMDocument::createTextNode() function in PHP:

Program 1:




<?php
  
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createTextNode() function to create text node
$domTN = $domDocument->createTextNode('neveropen');
  
// Append element to the document
$domDocument->appendChild($domTN);
  
// Create XML document and display it
echo $domDocument->saveXML();
  
?>


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
neveropen

Program 2:




<?php
  
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createTextNode() function to create text node
$domTN1 = $domDocument->createTextNode("neveropen");
$domTN2 = $domDocument->createTextNode("Noida");
$domTN3 = $domDocument->createTextNode("abc@geeksforgeeks.org");
  
// Append element to the document
$domDocument->appendChild($domTN1);
$domDocument->appendChild($domTN2);
$domDocument->appendChild($domTN3);
  
// Create XML document and display it
echo $domDocument->saveXML();
  
?>


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
neveropen
Noida
abc@geeksforgeeks.org

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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS