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

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 the cdata.

Return Value: This function returns the new DOMCDATASection on success or FALSE on failure.

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

Program 1:




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


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
<![CDATA[neveropen]]>

Program 2:




<?php
  
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createComment() function to create a new comment node
$domComment = $domDocument->createComment('Create CDATA file');
  
// Use createCDATASection() function to create a new cdata node
$domElement = $domDocument->createCDATASection('neveropen');
  
// Append element to the document
$domDocument->appendChild($domComment);
$domDocument->appendChild($domElement);
  
// Save the XML file and display it
echo $domDocument->saveXML();
  
?>


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
<!--Create CDATA file-->
<![CDATA[neveropen]]>

Reference: https://www.php.net/manual/en/domdocument.createcdatasection.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