PHP xml_set_default_handler() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
XML Parser

Definition

In PHP, xml_set_default_handler() registers a fallback function for parts of an XML document that no other handler takes care of, such as comments.

The parser sends each piece of the document to the most specific handler you set. Whatever is left over goes to the default handler. With a start and end element handler and a character data handler in place, the default handler mostly receives comments.

Syntax

Call xml_set_default_handler() with the values you need. Start with this line.

PHP
xml_set_default_handler($parser, function (XMLParser $parser, string $data) {
    echo $data;
});
CallWhat it does
xml_set_default_handler($parser, 'onOther')Uses the global function onOther() as the fallback handler.
xml_set_default_handler($parser, [$reader, 'onOther'])Uses the onOther() method of the $reader object.
xml_set_default_handler($parser, fn ($p, $d) => print($d))Uses a short arrow function that prints the raw text it receives.

Your handler is called as handler(XMLParser $parser, string $data), where $data is the raw text of the piece, for example <!--note-->. Beginners rarely need this handler. Element and character data handlers cover everyday XML reading.

Library declarations
PHP
xml_set_default_handler(XMLParser $parser, callable $handler): true

Parameters

ParameterDescription
parserRequired. The XMLParser object created by xml_parser_create() or xml_parser_create_ns().
handlerRequired. A callable that accepts the XMLParser as its first argument and the raw text as its second argument.

Return Value

Return ValueDescription
trueAlways returns true after the handler is stored.

PHP Examples

Example 1 — Catch a Comment

Set element, character data, and default handlers, then see that the comment is the only thing the default handler receives.

PHP
<?php
$xml = '<list><!--first--><item>A</item></list>';

$other = [];
$text = '';
$parser = xml_parser_create();

xml_set_element_handler(
    $parser,
    function (XMLParser $p, string $name) {
    },
    function (XMLParser $p, string $name) {
    }
);

xml_set_character_data_handler(
    $parser,
    function (XMLParser $p, string $data) use (&$text) {
        $text .= $data;
    }
);

xml_set_default_handler(
    $parser,
    function (XMLParser $p, string $data) use (&$other) {
        $other[] = $data;
    }
);

xml_parse($parser, $xml, true);

echo 'Default: ', implode(' | ', $other), PHP_EOL;
echo 'Text: ', $text, PHP_EOL;

How It Works

  1. The element handlers take care of every start and end tag, so tags never reach the default handler.
  2. The character data handler collects the text A.
  3. The comment has no handler of its own, so the parser passes it to the default handler.
  4. The default handler receives the raw comment text, including the comment markers.

Example 2 — A Comment Splits the Text

A comment in the middle of an element breaks its text into two pieces, and the default handler receives the comment between them.

PHP
<?php
$xml = '<note>Hi<!--skip-->there</note>';

$events = [];
$parser = xml_parser_create();

xml_set_element_handler(
    $parser,
    function (XMLParser $p, string $name) {
    },
    function (XMLParser $p, string $name) {
    }
);

xml_set_character_data_handler(
    $parser,
    function (XMLParser $p, string $data) use (&$events) {
        $events[] = 'text ' . $data;
    }
);

xml_set_default_handler(
    $parser,
    function (XMLParser $p, string $data) use (&$events) {
        $events[] = 'default ' . $data;
    }
);

xml_parse($parser, $xml, true);

foreach ($events as $event) {
    echo $event, PHP_EOL;
}

How It Works

  1. The note element contains the words Hi and there with a comment between them.
  2. The parser sends Hi to the character data handler first.
  3. The comment goes to the default handler, because nothing else handles comments.
  4. The word there arrives afterwards, so one element produced two separate text events.

Example 3 — See the Order of Events

Log every handler into one list to see the order in which the parser calls them.

PHP
<?php
$xml = '<doc><!--note--><item>A</item></doc>';

$log = [];
$parser = xml_parser_create();

xml_set_element_handler(
    $parser,
    function (XMLParser $p, string $name) use (&$log) {
        $log[] = 'start ' . $name;
    },
    function (XMLParser $p, string $name) use (&$log) {
        $log[] = 'end ' . $name;
    }
);

xml_set_character_data_handler(
    $parser,
    function (XMLParser $p, string $data) use (&$log) {
        $log[] = 'text ' . $data;
    }
);

xml_set_default_handler(
    $parser,
    function (XMLParser $p, string $data) use (&$log) {
        $log[] = 'default ' . $data;
    }
);

xml_parse($parser, $xml, true);

echo implode(PHP_EOL, $log), PHP_EOL;

How It Works

  1. All four handlers write a short label into the same log array.
  2. The parser reads the document from top to bottom and calls the matching handler at each step.
  3. The comment inside doc is the only piece that reaches the default handler.
  4. implode() prints the log, which shows the exact order of the events.

Frequently Asked Questions

It registers a fallback function. The parser calls it for pieces of the XML document that have no more specific handler, such as comments.
It is called for anything the other handlers do not take care of. If you set element and character data handlers, comments are the main thing left over.
It receives the XMLParser object and a string with the raw text of the piece, for example <!--note-->, including the comment markers.
No. Most scripts only need xml_set_element_handler() and xml_set_character_data_handler(). Add a default handler only when you care about comments or other leftovers.
It returns true in PHP 8. The call only stores the handler, so there is no error to check.

Next: xml_set_element_handler()

xml_set_element_handler() registers the two functions that run when the parser meets an op.

xml_set_element_handler() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful