Bug->Doc #73670 [Opn]: simplexml_load_string returns `FALSE` but does not set error (for empty strings
| From: | requinix@php.net | Date: | Wed, 07 Dec 2016 02:15:17 +0000 |
| Subject: | Bug->Doc #73670 [Opn]: simplexml_load_string returns `FALSE` but does not set error (for empty strings | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-14179@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73670&edit=1
ID: 73670
Updated by: requinix@php.net
Reported by: zerkms at zerkms dot ru
Summary: simplexml_load_string returns
FALSE but does not
set error (for empty strings
Status: Open
-Type: Bug
+Type: Documentation Problem
Package: SimpleXML related
Operating System: Irrelevant
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2016-12-07 01:18:41] zerkms at zerkms dot ru
Description:
------------
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');
libxml_use_internal_errors($previous);
$result = libxml_get_last_error();
$previous = libxml_use_internal_errors(true);
$doc = simplexml_load_string('');
libxml_use_internal_errors($previous);
$result = libxml_get_last_error();
var_dump($doc, $result);
Expected result:
----------------
bool(false)
bool(false)
Actual result:
--------------
bool(false)
object(LibXMLError)#2 (6) {
["level"]=>
int(3)
["code"]=>
int(4)
["column"]=>
int(1)
["message"]=>
string(34) "Start tag expected, '<' not found
"
["file"]=>
string(0) ""
["line"]=>
int(1)
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73670&edit=1