Bug #81443 [Opn->Nab]: Inconsistent casting to bool of SimpleXML objects

From: Date: Wed, 15 Sep 2021 22:52:08 +0000
Subject: Bug #81443 [Opn->Nab]: Inconsistent casting to bool of SimpleXML objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-236635@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81443&edit=1 ID: 81443 Updated by: cmb@php.net Reported by: rk at srsbiz dot pl Summary: Inconsistent casting to bool of SimpleXML objects -Status: Open +Status: Not a bug Type: Bug Package: SimpleXML related Operating System: * PHP Version: Irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: > Not sure if this is SimpleXML or documentation problem Neither. The three cases yielding true are not about attributeless empty elements, since empty element implies that there are no children, but <root> has child elements. Previous Comments: ------------------------------------------------------------------------ [2021-09-15 21:42:46] rk at srsbiz dot pl Description: ------------ As documentation says https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting > SimpleXML objects created from attributeless empty elements are considered false, but in some cases such nodes are evaluated as true https://3v4l.org/qIH4a Not sure if this is SimpleXML or documentation problem Test script: --------------- <?php var_dump( (bool)simplexml_load_string('<root />'), (bool)simplexml_load_string('<root><branch /></root>')->branch, (bool)simplexml_load_string('<root><branch></branch></root>')->branch, (bool)simplexml_load_string('<root><otherbranch /><branch /></root>')->branch, (bool)simplexml_load_string('<root><branch /><branch /></root>')->branch[0], (bool)simplexml_load_string('<root><branch /><branch></branch></root>')->branch[1], ); Expected result: ---------------- bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) Actual result: -------------- bool(false) bool(true) bool(true) bool(true) bool(false) bool(false) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81443&edit=1

« previous php.bugs (#236635) next »