PHP DOMDocument createProcessingInstruction() Function

Last Updated : 03 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The DOMDocument::createProcessingInstruction() function is an inbuilt function in PHP that is used to create a new instance of the class DOMProcessingInstruction.

Syntax:

DOMProcessingInstruction DOMDocument::createProcessingInstruction( $target, $data )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $target: This parameter holds the target of the processing instruction.
  • $data: This parameter holds the content of the processing instruction.

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

Below examples illustrate the DOMDocument::createProcessingInstruction() function in PHP:

Example 1:

php
<?php

// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');

// Use createProcessingInstruction() function to create
// a new Processing Instruction node
$domPI = $domDocument->createProcessingInstruction("name", "GeeksforGeeks");

// Append element to the document
$domDocument->appendChild($domPI);

// Create XML document and display it
echo $domDocument->saveXML();

?>
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<?name GeeksforGeeks?>

Example 2:

php
<?php

// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');

// Use createProcessingInstruction() function to create
// a new Processing Instruction node
$domPI1 = $domDocument->createProcessingInstruction("name",
                     "GeeksforGeeks");
$domPI2 = $domDocument->createProcessingInstruction("contact",
                     "Noida");
$domPI3 = $domDocument->createProcessingInstruction("email",
                     "abc@geeksforgeeks.org");

// Append element to the document
$domDocument->appendChild($domPI1);
$domDocument->appendChild($domPI2);
$domDocument->appendChild($domPI3);

// Create XML document and display it
echo $domDocument->saveXML();

?>
Output:
<?xml version="1.0" encoding="iso-8859-1"?>
<?name GeeksforGeeks?>
<?contact Noida?>
<?email abc@geeksforgeeks.org?>

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


Next Article

Similar Reads