php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #69596 simplexml_load_string can return SimpleXML object that resolves to FALSE
Submitted: 2015-05-07 15:33 UTC Modified: 2015-06-14 14:08 UTC
From: meustrus at gmail dot com Assigned: cmb (profile)
Status: Closed Package: SimpleXML related
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
 [2015-05-07 15:33 UTC] 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

Patches

simplexml-false-problem (last revision 2015-05-07 22:46 UTC by cmb@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-07 22:32 UTC] cmb@php.net
-PHP Version: 5.4.40 +PHP Version: Irrelevant
 [2015-05-07 22:33 UTC] cmb@php.net
-Status: Open +Status: Verified -Operating System: OS X +Operating System: *
 [2015-05-07 22:33 UTC] cmb@php.net
Verified: <http://3v4l.org/uUUDH>.
 [2015-05-07 22:38 UTC] cmb@php.net
Related to bug #30972, bug #31045 and to user note #117237.
 [2015-05-07 22:46 UTC] cmb@php.net
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
 [2015-06-14 14:06 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=336942
Log: added return.falseproblem warning to SimpleXML factory functions (fixes bug #69596)
 [2015-06-14 14:07 UTC] cmb@php.net
-Status: Verified +Status: Closed -Assigned To: +Assigned To: cmb
 [2015-06-14 14:07 UTC] cmb@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.


 [2015-06-14 14:08 UTC] cmb@php.net
Disregard the comment above (wrong snippet).

This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 11:01:28 2024 UTC