Function name: libxml_get_last_error()
Applicable version: PHP 5, PHP 7
Usage: The libxml_get_last_error() function is used to obtain the error information of the last libxml error.
Syntax: libxml_get_last_error(): \LibXMLError|false
Return Value: This function returns a \LibXMLError object containing details of the most recent libxml error. If no error occurs, false is returned.
Example:
// 创建一个有错误的XML 文档$xml = "<root><element>"; // 禁用错误报告libxml_use_internal_errors(true); // 加载XML $doc = new DOMDocument(); $doc->loadXML($xml); // 获取最近一次libxml 错误$error = libxml_get_last_error(); if ($error) { echo "发生了一个libxml 错误:\n"; echo "代码:" . $error->code . "\n"; echo "消息:" . $error->message . "\n"; echo "行号:" . $error->line . "\n"; echo "列号:" . $error->column . "\n"; } else { echo "没有发生libxml 错误。\n"; }
Output result:
发生了一个libxml 错误:代码:76消息:Opening and ending tag mismatch: element line 1 and root行号:1列号:17
In the example above, we created an XML document with errors and loaded the XML using the loadXML() method of the DOMDocument class. Then, we use the libxml_get_last_error() function to get the details of the last libxml error. If an error occurs, we print out the error code, message, line number and column number. If no error occurs, we print out a corresponding message.
Note that before using the libxml_get_last_error() function, we call libxml_use_internal_errors(true) to disable error reporting, so that the libxml error message will be returned. If the function is not called, the libxml error will be directly output to the standard error stream.
libxml_get_last_error ( )