From:             dtorop932 at gmail dot com
Operating system: GNU/Linux 2.4.27
PHP version:      5CVS-2004-12-03 (dev)
PHP Bug Type:     SimpleXML related
Bug description:  Casting an empty single node SimpleXML object to boolean 
returns false

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 bug report at http://bugs.php.net/?id=30972&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=30972&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=30972&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=30972&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=30972&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=30972&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=30972&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=30972&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=30972&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=30972&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=30972&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=30972&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=30972&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=30972&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=30972&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=30972&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=30972&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=30972&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=30972&r=float
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=30972&r=mysqlcfg

Reply via email to