Doc #69596 [Opn->Ver]: simplexml_load_string can return SimpleXML object that resolves to FALSE
| From: | cmb@php.net | Date: | Thu, 07 May 2015 22:33:15 +0000 |
| Subject: | Doc #69596 [Opn->Ver]: 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-12306@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
Updated by: cmb@php.net
Reported by: meustrus at gmail dot com
Summary: simplexml_load_string can return SimpleXML object
that resolves to FALSE
-Status: Open
+Status: Verified
Type: Documentation Problem
Package: SimpleXML related
-Operating System: OS X
+Operating System: *
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Verified: <http://3v4l.org/uUUDH>.
Previous Comments:
------------------------------------------------------------------------
[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