PHP libxml_get_last_error() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
libxml

Definition

In PHP, libxml_get_last_error() returns the most recent libxml error as a LibXMLError object. If there are no errors, it returns false.

It is a quick way to see why a parse failed without looping through the whole error list. It works well with libxml_use_internal_errors(true).

Syntax

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

PHP
$error = libxml_get_last_error();
CallWhat it does
libxml_get_last_error()Returns the latest LibXMLError, or false when no error exists.
libxml_get_last_error()->messageReads the message of the latest error (check for false first).
libxml_get_last_error() === falseTrue when nothing has gone wrong.

The returned object has the properties level, code, column, message, file, and line. The message usually ends with a newline, so wrap it with trim() when printing.

Library declarations
PHP
libxml_get_last_error(): LibXMLError|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
LibXMLError|falseThe last error as a LibXMLError object, or false if the error buffer is empty.

PHP Examples

Example 1 — Read the Last Error

Parse XML with a missing closing tag and print the message from libxml_get_last_error().

PHP
<?php
libxml_use_internal_errors(true);

$xml = simplexml_load_string('<root><item>Hello</item>');

if ($xml === false) {
    $error = libxml_get_last_error();
    echo trim($error->message), PHP_EOL;
}

How It Works

  1. Internal errors are enabled so nothing is printed automatically.
  2. The root tag is never closed, so simplexml_load_string() returns false.
  3. libxml_get_last_error() returns the LibXMLError for that problem.
  4. trim() removes the trailing newline from the message before printing.

Example 2 — No Errors Returns False

When parsing succeeds, there is no last error, and the function returns false.

PHP
<?php
libxml_use_internal_errors(true);

simplexml_load_string('<ok>All good</ok>');

var_dump(libxml_get_last_error());

How It Works

  1. The XML is well formed, so no errors are stored.
  2. libxml_get_last_error() has nothing to return.
  3. It returns false instead of an object.
  4. This is why you should check the result before using its properties.

Example 3 — Use It with DOMDocument

DOMDocument::loadXML() returns false on failure, and libxml_get_last_error() explains why.

PHP
<?php
libxml_use_internal_errors(true);

$dom = new DOMDocument();
$loaded = $dom->loadXML('<root><item>Hello</item>');

if (!$loaded) {
    $error = libxml_get_last_error();
    echo "Load failed: ", trim($error->message), PHP_EOL;
}

libxml_clear_errors();

How It Works

  1. DOMDocument uses libxml behind the scenes, so the same error buffer applies.
  2. loadXML() returns false because the root element is not closed.
  3. libxml_get_last_error() gives the reason as a LibXMLError object.
  4. libxml_clear_errors() empties the buffer after we are done.

Frequently Asked Questions

It returns the most recent libxml error as a LibXMLError object. If no error has happened, it returns false.
It is strongly recommended. Without it, libxml prints warnings straight to the output, which is noisy and harder to handle in your code.
libxml_get_last_error() gives only the newest error. libxml_get_errors() gives the whole list as an array.
libxml adds a line break to its messages. Use trim() or rtrim() to remove it before showing the message to users.
Call libxml_clear_errors(). After that, libxml_get_last_error() returns false until a new error happens.

Next: libxml_set_external_entity_loader()

libxml_set_external_entity_loader() lets you register your own function that decides how l.

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