|  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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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-2024 The PHP Group
All rights reserved.
Last updated: Wed Jul 17 23:01:28 2024 UTC