PHP xml_parser_free() Function

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

Definition

In PHP, xml_parser_free() is a legacy function that used to release an XML parser from memory.

Before PHP 8 the parser was a resource that had to be freed by hand. In PHP 8 the parser is an XMLParser object, so PHP cleans it up on its own when nothing uses it anymore. The function is usually unnecessary today.

Syntax

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

PHP
xml_parser_free($parser);
CallWhat it does
xml_parser_free($parser)Legacy call that returns true. It does nothing useful in PHP 8.
unset($parser)The modern way to let go of the parser, so the object can be cleaned up.
$parser = nullAnother way to drop the reference when you are finished.

In PHP 8 xml_parser_free() does not destroy the object. It is kept only for old code. Newer PHP versions (8.5 and later) mark the function as deprecated, so you can safely leave it out of new code.

Library declarations
PHP
xml_parser_free(XMLParser $parser): bool

Parameters

ParameterDescription
parserRequired. The XMLParser object created by xml_parser_create() or xml_parser_create_ns().

Return Value

Return ValueDescription
booltrue on success. It can return false with a warning when called while the parser is still parsing.

PHP Examples

Example 1 — The Legacy Call

Older tutorials free the parser after parsing. On PHP 8.0 to 8.4 it still works and returns true. A version check keeps the example safe on newer releases.

PHP
<?php
$parser = xml_parser_create();
xml_parse($parser, '<root><item id="1">Tea</item></root>', true);

if (PHP_VERSION_ID < 80500) {
    $freed = xml_parser_free($parser);
    echo "xml_parser_free() returned: ", var_export($freed, true), PHP_EOL;
}

How It Works

  1. The parser is created and used to parse a tiny XML string.
  2. The PHP_VERSION_ID check skips the legacy call on PHP 8. 5 and later.
  3. xml_parser_free() returns true on older versions.
  4. var_export() prints the boolean as the word true.

Example 2 — The Object Is Still There

In PHP 8 the function does not remove the object. The variable still holds an XMLParser after the call.

PHP
<?php
$parser = xml_parser_create();

if (PHP_VERSION_ID < 80500) {
    xml_parser_free($parser);
}

echo "Still an XMLParser: ", $parser instanceof XMLParser ? "yes" : "no", PHP_EOL;

How It Works

  1. xml_parser_create() returns an XMLParser object, not a resource.
  2. The legacy xml_parser_free() call only runs on older PHP 8 versions.
  3. The object is cleaned up only when no variable points to it anymore.
  4. instanceof shows that $parser is still an XMLParser.

Example 3 — No Cleanup Needed in a Loop

Create a parser for each document and let PHP clean up the old ones. No xml_parser_free() call is needed.

PHP
<?php
for ($i = 1; $i <= 3; $i++) {
    $parser = xml_parser_create();
    $xml = '<root><item id="' . $i . '">Tea</item></root>';
    $ok = xml_parse($parser, $xml, true);

    echo "Document ", $i, ": ", $ok, PHP_EOL;
}

How It Works

  1. The loop builds a new XML string for each value of $i.
  2. Each pass creates a fresh parser and parses one document.
  3. When $parser is replaced, the previous object is no longer used and PHP cleans it up.
  4. Every document prints 1, which means the parse succeeded.

Frequently Asked Questions

Almost nothing. It is a legacy function that returns true. The XMLParser object is cleaned up automatically when it is no longer used.
Usually no. In PHP 8 you can leave it out, or use unset($parser) if you want to drop the variable early.
Newer PHP versions (8.5 and later) mark it as deprecated because it has no real effect. It is best left out of new code.
Before PHP 8 the parser was a resource that had to be freed manually. Those tutorials were written for that older behaviour.
Nothing bad. PHP releases the XMLParser object when the variable goes out of scope or the script ends.

Next: xml_parser_get_option()

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

xml_parser_get_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