|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73670 simplexml_load_string returns `FALSE` but does not set error (for empty strings
Submitted: 2016-12-07 01:18 UTC Modified: 2016-12-07 02:15 UTC
From: zerkms at zerkms dot ru Assigned:
Status: Open Package: SimpleXML related
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: zerkms at zerkms dot ru
New email:
PHP Version: OS:


 [2016-12-07 01:18 UTC] zerkms at zerkms dot ru
`simplexml_load_string()` documentation states

> Returns an object of class SimpleXMLElement with properties containing the data held within the xml document, or FALSE on failure.

So I expect if `FALSE` is returned - it was failed to parse xml, so the `libxml_get_last_error` returns the actual error.

What happens instead - for the empty string the `simplexml_load_string` returns `false` and `libxml_get_last_error` returns previous non actual error.

It should be either a documentation additionally clarify the case, or the implementation behave accordingly.

Test script:
$previous = libxml_use_internal_errors(true);
$doc = simplexml_load_string('foo');

$result = libxml_get_last_error();

$previous = libxml_use_internal_errors(true);
$doc = simplexml_load_string('');

$result = libxml_get_last_error();

var_dump($doc, $result);

Expected result:

Actual result:
object(LibXMLError)#2 (6) {
  string(34) "Start tag expected, '<' not found
  string(0) ""


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-07 02:15 UTC]
-Type: Bug +Type: Documentation Problem
 [2016-12-07 02:15 UTC]
libxml2 does not set error information if the input string is empty. It would be easy to update the Return Values to indicate that parsing an empty string returns false with no additional error messages.

Error information does not clear automatically. Since no new errors were added, libxml_get_last_error() will return the error from the previous call. Use libxml_clear_errors() to avoid confusion.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Sep 22 22:01:41 2021 UTC