PHP libxml_use_internal_errors() Function
The PHP libxml_use_internal_errors() function disables the standard libxml errors and enables user error handling.
Syntax
libxml_use_internal_errors(use_errors)
Parameters
use_errors |
Optional. Enable user error handling (true) or disable user error handling (false). Disabling will also clear any existing libxml errors. |
Note: Since PHP 8.0.0, use_errors is nullable now. Previously, its default was false.
Return Value
Returns the previous value of use_errors.
Example:
The example below shows how to build a simple libxml error handler.
<?php libxml_use_internal_errors(true); //contains mismatched tags $xmlstr = <<<XML <mail> <To>John Smith</too> <From>Marry G.</From> <Subject>Happy Birthday</Subject> <body>Happy birthday. Live your life with smiles.</body> </mail> XML; $doc = simplexml_load_string($xmlstr); $xml = explode("\n", $xmlstr); if ($doc === false) { $errors = libxml_get_errors(); //displaying an error occurred foreach ($errors as $error) { echo display_xml_error($error, $xml); } //clearing the libxml error buffer libxml_clear_errors(); } //function to display error message function display_xml_error($error, $xml) { $message = ""; switch ($error->level) { case LIBXML_ERR_WARNING: $message .= "Warning $error->code: "; break; case LIBXML_ERR_ERROR: $message .= "Error $error->code: "; break; case LIBXML_ERR_FATAL: $message .= "Fatal Error $error->code: "; break; } $message .= trim($error->message) . "\n Line: $error->line" . "\n Column: $error->column"; if ($error->file) { $message .= "\n File: $error->file"; } return $message; } ?>
The output of the above code will be:
Fatal Error 76: Opening and ending tag mismatch: To line 2 and too Line: 2 Column: 23
❮ PHP Libxml Reference