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();
Call
What it does
libxml_get_last_error()
Returns the latest LibXMLError, or false when no error exists.
libxml_get_last_error()->message
Reads the message of the latest error (check for false first).
libxml_get_last_error() === false
True 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
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
LibXMLError|false
The 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().