W3docs

xml_parser_get_option()

The xml_parser_get_option() function is a PHP built-in function that gets an option set on an XML parser. When parsing XML files using the SimpleXML library or

The xml_parser_get_option() function reads the current value of a configuration option on an XML parser created with the legacy Expat-based xml extension. Each parser holds a small set of options (case folding, target encoding, and whitespace skipping) that control how it turns raw markup into events. You set these with xml_parser_set_option(); you read them back with xml_parser_get_option().

In practice you reach for it to confirm a parser's configuration before you start parsing — for example, to check whether case folding is still on, or to log which target encoding a parser will produce. It is the read-only companion to the setter and never changes the parser.

This page covers the function's signature, the options you can query (and their defaults), a runnable example, and the gotchas worth knowing — including the fact that Expat is a low-level API most projects no longer reach for directly.

Syntax

xml_parser_get_option(XMLParser $parser, int $option): string|int|bool

$parser is the parser handle returned by xml_parser_create() (since PHP 8.0 this is an XMLParser object rather than a resource). $option is one of the XML_OPTION_* constants below. The return type depends on the option: a boolean for the on/off flags, an integer for XML_OPTION_SKIP_TAGSTART, and a string for XML_OPTION_TARGET_ENCODING.

Available options

ConstantTypeDefaultWhat it controls
XML_OPTION_CASE_FOLDINGbooltrueWhether element/attribute names are upper-cased before they reach your handlers.
XML_OPTION_TARGET_ENCODINGstring"UTF-8"The encoding the parser converts parsed data into.
XML_OPTION_SKIP_WHITEboolfalseWhether whitespace-only character data is skipped.
XML_OPTION_SKIP_TAGSTARTint0How many characters to skip from the start of a tag name.

Querying an unknown option returns false.

Examples

Reading a single option

This creates a parser, turns case folding off, then reads the option back. Because false is returned, the message reports "disabled":

<?php
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);

$caseFolding = xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING);
echo "Case folding is " . ($caseFolding ? "enabled" : "disabled") . ".\n";

Output:

Case folding is disabled.

Inspecting defaults and confirming changes

A fresh parser already has sensible defaults. Reading them before and after a set call is a good way to verify your configuration took effect:

<?php
$parser = xml_parser_create();

// Defaults on a brand-new parser.
echo "Default case folding: ";
var_dump(xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING));   // bool(true)
echo "Default encoding: ";
var_dump(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING)); // string(5) "UTF-8"

// Change two options, then confirm them.
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, true);

printf("Case folding now: %s\n", xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING) ? "on" : "off");
printf("Skip whitespace:  %s\n", xml_parser_get_option($parser, XML_OPTION_SKIP_WHITE) ? "on" : "off");

Output:

Default case folding: bool(true)
Default encoding: string(5) "UTF-8"
Case folding now: off
Skip whitespace:  on

Set your options first, verify them with xml_parser_get_option() if you like, and only then feed data to xml_parse(). Once parsing has started, changing the case-folding or encoding options is no longer allowed.

Notes and gotchas

  • It only reads. This function never mutates the parser. To change an option, use xml_parser_set_option().
  • Returns mixed types. Treat the result by option: a bool flag, an int for SKIP_TAGSTART, or a string for the encoding. Don't assume a boolean for every option.
  • Expat is low-level and dated. The xml (Expat) extension predates modern APIs. Unless you specifically need event-based ("SAX") parsing of very large documents, SimpleXML or DOM are usually easier and safer. Note that XML_OPTION_CASE_FOLDING defaulting to true is the classic surprise: element names arrive upper-cased unless you turn it off.
  • Free the parser when done. Although PHP frees the XMLParser object on its own, you can call xml_parser_free() to release it explicitly.

Conclusion

xml_parser_get_option() is the getter half of the Expat parser's option pair: it returns the current value of XML_OPTION_CASE_FOLDING, XML_OPTION_TARGET_ENCODING, XML_OPTION_SKIP_WHITE, or XML_OPTION_SKIP_TAGSTART without changing anything. Use it to confirm a parser's configuration before calling xml_parse(). For new code that doesn't need low-level SAX parsing, prefer SimpleXML or the DOM extension instead.

Practice

Practice
What does the xml_parser_get_option() function in PHP do?
What does the xml_parser_get_option() function in PHP do?
Was this page helpful?