Doc #69596 [PATCH]: simplexml_load_string can return SimpleXML object that resolves to FALSE
| From: | cmb@php.net | Date: | Thu, 07 May 2015 22:46:35 +0000 |
| Subject: | Doc #69596 [PATCH]: simplexml_load_string can return SimpleXML object that resolves to FALSE | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-12308@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69596&edit=1
ID: 69596
Patch added by: cmb@php.net
Reported by: meustrus at gmail dot com
Summary: simplexml_load_string can return SimpleXML object
that resolves to FALSE
Status: Verified
Type: Documentation Problem
Package: SimpleXML related
Operating System: *
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
The following patch has been added/updated:
Patch Name: simplexml-false-problem
Revision: 1431038794
URL: https://bugs.php.net/patch-display.php?bug=69596&patch=simplexml-false-problem&revision=1431038794
Previous Comments:
------------------------------------------------------------------------
[2015-05-07 22:38:46] cmb@php.net
Related to bug #30972, bug #31045 and to user note #117237.
------------------------------------------------------------------------
[2015-05-07 22:33:14] cmb@php.net
Verified: <http://3v4l.org/uUUDH>.
------------------------------------------------------------------------
[2015-05-07 15:33:14] meustrus at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.simplexml-load-string
---
The
simplexml_load_string function returns a SimpleXML object or FALSE. However under
certain conditions it may return a SimpleXML object that resolves as FALSE, complicating error
handling. Spcifically, this happens when all elements in the document are namespaced, resulting in
no publicly visible state in the SimpleXML object.
The documentation should be updated with a warning similar to that for strpos that when
checking for failure, the output must be compared to FALSE with === instead of simply resolved using
PHP's casting rules.
Test script:
---------------
<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<ns1:Root xmlns:ns1="http://example.com/custom">
<ns1:Node>There's stuff here</ns1:Node>
</ns1:Root>
XML;
$simplexml = simplexml_load_string($xml);
// This prints "Parse Error".
echo ($simplexml ? 'Valid XML' : 'Parse Error'), PHP_EOL;
// But this prints "There's stuff here", proving that
// the SimpleXML object was created successfully.
echo $simplexml->children('http://example.com/custom')->Node, PHP_EOL;
// The documentation should instruct us to use this instead:
echo ($simplexml !== FALSE ? 'Valid XML' : 'Parse Error'), PHP_EOL;
Expected result:
----------------
Valid XML
There's stuff here
Valid XML
Actual result:
--------------
Parse Error
There's stuff here
Valid XML
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69596&edit=1