|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-07-29 10:50 UTC] sjon at react dot com
Description:
------------
I understand the bug I am reporting is described in the manual, which is why I am reporting it as a feature request. When casting SimpleXMLElements to boolean, most values comply with the way PHP casts them. There is however one exception: the string '0' is cast to true, which is unexpected. This is especially annoying when using XmlRpc, which uses <boolean>0</boolean> for its values.
Test script:
---------------
<?php
var_dump((bool)new SimpleXMLElement("<w>0</w>"));
Expected result:
----------------
bool(false)
Actual result:
--------------
bool(true)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 08:00:01 2025 UTC |
Then why does this work correctly? var_dump((int)new SimpleXMLElement("<w>2</w>")); (returns int(2)) The reason is SimpleXMLElements do not follow the normal rules, have a look at simplexml.c line 1762: if (type == IS_BOOL) { node = php_sxe_get_first_node(sxe, NULL TSRMLS_CC); prop_hash = sxe_get_prop_hash(readobj, 1 TSRMLS_CC); INIT_PZVAL(writeobj); ZVAL_BOOL(writeobj, node != NULL || zend_hash_num_elements(prop_hash) > 0); zend_hash_destroy(prop_hash); efree(prop_hash); return SUCCESS; }