Req #52482 [Fbk->NoF]: Casting SimpleXMLElement('0') to boolean results in true

From: Date: Sun, 11 Jul 2021 04:22:07 +0000
Subject: Req #52482 [Fbk->NoF]: Casting SimpleXMLElement('0') to boolean results in true
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234945@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=52482&edit=1 ID: 52482 Updated by: php-bugs@lists.php.net Reported by: sjon at react dot com Summary: Casting SimpleXMLElement('0') to boolean results in true -Status: Feedback +Status: No Feedback Type: Feature/Change Request Package: SimpleXML related Operating System: Linux PHP Version: Irrelevant Assigned To: cmb Private report: N New Comment: No feedback was provided. The bug is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so and change the status of the bug back to "Re-Opened". Thank you. Previous Comments: ------------------------------------------------------------------------ [2021-07-02 16:52:03] cmb@php.net Given that SimpleXMLElements behave this way since they have been introduced, changing their behavior would require an RFC. Not sure what to do with this ticket. ------------------------------------------------------------------------ [2010-07-29 12:40:44] salathe@php.net I didn't mean to suggest this wasn't worth considering. I'm just wondering whether this particular suggestion might cause some impact for folks who already make use of the current behaviour. ------------------------------------------------------------------------ [2010-07-29 11:12:43] sjon at react dot com 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; } ------------------------------------------------------------------------ [2010-07-29 11:02:50] salathe@php.net You are not casting the string "0" to boolean, you're casting the object. As such, it follows normal rules for casting objects. The only time a SimpleXMLElement will cast to boolean false is with an "empty" element (e.g. "<w/>"). If you want your sample element to cast to false, you will first need to cast to a string: (bool) (string) new SimpleXMLElement... ------------------------------------------------------------------------ [2010-07-29 10:50:11] 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) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=52482&edit=1

« previous php.bugs (#234945) next »