#30972 [Bgs->Csd]: Casting an empty single node SimpleXML object to boolean returns false
| From: | simp@php.net | Date: | Thu, 30 Mar 2006 02:29:43 +0000 |
| Subject: | #30972 [Bgs->Csd]: Casting an empty single node SimpleXML object to boolean returns false | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-95099@lists.php.net to get a copy of this message | ||
ID: 30972
Updated by: simp@php.net
Reported By: dtorop932 at gmail dot com
-Status: Bogus
+Status: Closed
Bug Type: SimpleXML related
Operating System: GNU/Linux 2.4.27
PHP Version: 5CVS-2004-12-03 (dev)
New Comment:
This bug has been fixed in CVS.
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/.
Thank you for the report, and for helping us make PHP better.
SimpleXML has its own cast handler which casts objects from empty tags
to false
Previous Comments:
------------------------------------------------------------------------
[2004-12-04 12:40:41] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Use ===, !== or is_null
------------------------------------------------------------------------
[2004-12-03 05:47:46] dtorop932 at gmail dot com
Description:
------------
According to http://www.php.net/manual/en/language.types.boolean.php
it
seems that in PHP 5 all objects should return TRUE when cast to boolean.
But if one creates a SimpleXML object from the string '<goodxml/>',
then casts it to boolean, the result is FALSE.
As a comparison, running DOMDocument::loadxml() on "<goodxml/>" and
casting the result to boolean returns TRUE.
This is important if one is doing what seems to be a quick test for
failure of SimpleXML parsing, e.g. the following doesn't work as
expected:
if (!simplexml_load_string($anyxml)) {
return FALSE; // this always executes, even for good XML
}
Reproduce code:
---------------
<?php
foreach (array('badxml', '<goodxml/>',
'<goodxml>CONTENT</goodxml>') as
$xml) {
$dom = @simplexml_load_string($xml);
print "\n\$dom from '$xml'\n";
print ' ((boolean) $dom) = '; var_dump((boolean) $dom);
}
?>
Expected result:
----------------
$dom from 'badxml'
((boolean) $dom) = bool(false)
$dom from '<goodxml/>'
((boolean) $dom) = bool(true)
$dom from '<goodxml>CONTENT</goodxml>'
((boolean) $dom) = bool(true)
Actual result:
--------------
$dom from 'badxml'
((boolean) $dom) = bool(false)
$dom from '<goodxml/>'
((boolean) $dom) = bool(false)
$dom from '<goodxml>CONTENT</goodxml>'
((boolean) $dom) = bool(true)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=30972&edit=1