Example 1 — Read One Processing Instruction
Print the target and the data of a processing instruction placed inside a tiny document.
<?php
$xml = '<doc><?app mode="fast"?><a/></doc>';
$parser = xml_parser_create();
xml_set_processing_instruction_handler(
$parser,
function (XMLParser $p, string $target, string $data) {
echo 'Target: ', $target, PHP_EOL;
echo 'Data: ', $data, PHP_EOL;
}
);
xml_parse($parser, $xml, true); How It Works
- The document contains one processing instruction with the target app.
- Everything after the target and a space is the data, here mode="fast".
- The parser calls the handler as soon as it reads the instruction.
- The handler prints the target and the data on separate lines.

