php8.5
Home/ Manual/ dom / domelement/ DOMElement::prepend

DOMElement::prepend

PHP function Edit on GitHub ✎

(PHP 8)

Prepends nodes before the first child node

Description

DOMElement::prepend(DOMNode|string ...$nodes): void

Prepends one or many nodes to the list of children before the first child node.

Examples

DOMElement::prepend example

Prepends nodes in the container element.

php
<?php
$doc = new DOMDocument;
$doc->loadXML("<container> world</container>");
$world = $doc->documentElement;

$world->prepend($doc->createElement("hello"), "beautiful");

echo $doc->saveXML();
?>

The above example will output:

output
<?xml version="1.0"?>
<container><hello/>beautiful world</container>

See Also

  • DOMParentNode::prepend
  • DOMElement::append

Source: reference/dom/domelement/prepend.xml · from the official PHP manual (php/doc-en)