php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31045 Testing return value of simplexml_load_string
Submitted: 2004-12-09 23:23 UTC Modified: 2004-12-15 14:47 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:2 of 3 (66.7%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: rbro at hotmail dot com Assigned:
Status: Wont fix Package: SimpleXML related
PHP Version: 5CVS-2004-12-09 (dev) OS: Linux
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-12-09 23:23 UTC] rbro at hotmail dot com
Description:
------------
The manual states that simplexml_load_string (and simplexml_load_file) returns false on any errors.  In the following example, I am loading a valid xml stream, but the simplexml object returned seems to equal false when it shouldn't.

Reproduce code:
---------------
<?php
$doc = simplexml_load_string('<xml></xml>');

if ($doc == false)
{
	echo 'equals false';
}
else
{
	echo 'does not equal false';
}
?>


Expected result:
----------------
does not equal false

Actual result:
--------------
equals false

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-09 23:53 UTC] fmk@php.net
PHP is loosly typed so comparing an object with a boolean will result in this behavior.

try comparing with === or check the type of the returned object.
 [2004-12-10 00:07 UTC] rbro at hotmail dot com
Ok, but then why does this work as expected (when a valid host, username, password, and db are supplied):

Reproduce code:
---------------
<?php
if (($db = mysqli_connect('localhost', 'user', 'password', 'db')) === false)
{
	echo 'equals false';
}
else
{
	echo 'does not equal false';
}
?>

Expected result:
----------------
does not equal false

Actual result:
--------------
does not equal false
 [2004-12-10 00:08 UTC] rbro at hotmail dot com
Sorry, that code example should have been the following (same result happens):

<?php
if (($db = mysqli_connect('localhost', 'user', 'password', 'db')) == false)
{
	echo 'equals false';
}
else
{
	echo 'does not equal false';
}
?>
 [2004-12-13 20:36 UTC] rbro at hotmail dot com
Just wondering if there is any update on this.  I really feel that this is a bug because there is a difference between the way the mysqli and simplexml objects work (between the mysqli_connect() and simplexml_load_string() functions) - since objects should work the same across the board.  Thanks.
 [2004-12-14 09:07 UTC] derick@php.net
If we have updates, we'll get back to you. No need to keep bugging us.
 [2004-12-15 13:04 UTC] chregu@php.net
really not a bug.

simplexml returns an empty string in your case (no text content in <xml>), which is == false in PHP's loosely typed implementation. Just use === and it works as expected
 [2004-12-15 14:47 UTC] rbro at hotmail dot com
Thanks - that makes sense with how simplexml works.  So others don't run into this, would it be possible add to the manual a warning about this on the simplexml_load_string and simplexml_load_file pages?  It's basically the same idea as the warning currently on http://www.php.net/strpos.  Thanks.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Thu Apr 17 06:02:13 2014 UTC