PHP xml_parser_get_option() Function

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

Definition

In PHP, xml_parser_get_option() reads the current value of one of the settings of an XML parser.

Each parser has a few options, such as case folding and the target encoding. Use this function to check what a parser is using now, and xml_parser_set_option() to change it.

Syntax

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

PHP
$value = xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING);
CallWhat it does
xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING)Returns 1 when tag names are folded to upper case, or 0 when they are not.
xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING)Returns the encoding name used for the data sent to your handlers.
xml_parser_get_option($parser, XML_OPTION_SKIP_WHITE)Returns whether whitespace-only text is skipped (0 or 1).

The available options are XML_OPTION_CASE_FOLDING, XML_OPTION_TARGET_ENCODING, XML_OPTION_SKIP_TAGSTART, and XML_OPTION_SKIP_WHITE. The return type depends on the option.

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

Parameters

ParameterDescription
parserRequired. The XMLParser object created by xml_parser_create() or xml_parser_create_ns().
optionRequired. One of the XML_OPTION_* constants.

Return Value

Return ValueDescription
stringThe target encoding name when you ask for XML_OPTION_TARGET_ENCODING.
intThe numeric value for the other options, such as 1 or 0.

PHP Examples

Example 1 — Check the Default Options

Create a parser and read two of its default settings.

PHP
<?php
$parser = xml_parser_create();

var_dump(xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING));
var_dump(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING));

How It Works

  1. xml_parser_create() makes a parser with the default options.
  2. Case folding is on by default, so the value is int(1).
  3. The target encoding is the encoding of the text your handlers receive.
  4. By default it is UTF-8, which is why the second line shows that string.

Example 2 — Invalid Options Throw ValueError

Passing a number that is not an XML_OPTION_* constant throws a ValueError. Catch it to see what happened.

PHP
<?php
$parser = xml_parser_create();

try {
    xml_parser_get_option($parser, 9999);
} catch (ValueError $e) {
    echo get_class($e), PHP_EOL;
}

How It Works

  1. 9999 is not one of the allowed option constants.
  2. PHP 8 throws a ValueError for an invalid option.
  3. The catch block receives the exception object.
  4. get_class() prints the class name, ValueError.

Example 3 — List All Four Options

Loop over the four option constants and print the default value of each one.

PHP
<?php
$parser = xml_parser_create();

$options = [
    'CASE_FOLDING' => XML_OPTION_CASE_FOLDING,
    'TARGET_ENCODING' => XML_OPTION_TARGET_ENCODING,
    'SKIP_TAGSTART' => XML_OPTION_SKIP_TAGSTART,
    'SKIP_WHITE' => XML_OPTION_SKIP_WHITE,
];

foreach ($options as $label => $option) {
    echo $label, ": ", xml_parser_get_option($parser, $option), PHP_EOL;
}

How It Works

  1. The array maps a readable label to each XML_OPTION_* constant.
  2. foreach goes through the labels and constants one at a time.
  3. xml_parser_get_option() returns the current value of each option.
  4. Only case folding is on by default. The others start at their basic values.

Frequently Asked Questions

It returns the current value of one parser option, for example whether tag names are folded to upper case.
XML_OPTION_CASE_FOLDING, XML_OPTION_TARGET_ENCODING, XML_OPTION_SKIP_TAGSTART, and XML_OPTION_SKIP_WHITE.
Case folding is 1, the target encoding is UTF-8, skip tag start is 0, and skip white is 0.
Use xml_parser_set_option() with the same constant, for example xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0).
PHP 8 throws a ValueError. Use the named XML_OPTION_* constants to avoid it.

Next: xml_parser_set_option()

xml_parser_set_option() changes a setting on an XML parser, such as case folding, tag-star.

xml_parser_set_option() 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