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:8
Avg. Score:3.5 ± 0.9
Reproduced:4 of 5 (80.0%)
Same Version:3 (75.0%)
Same OS:3 (75.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: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rbro at hotmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC