Bug #81443 [Com]: Inconsistent casting to bool of SimpleXML objects
| From: | a at b dot c dot de | Date: | Thu, 16 Sep 2021 00:25:32 +0000 |
| Subject: | Bug #81443 [Com]: Inconsistent casting to bool of SimpleXML objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-236636@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
Comment by: a at b dot c dot de
Reported by: rk at srsbiz dot pl
Summary: Inconsistent casting to bool of SimpleXML objects
Status: Not a bug
Type: Bug
Package: SimpleXML related
Operating System: *
PHP Version: Irrelevant
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
It's an infelicity of the SimpleXML interface that "->child" means almost, but not
quite, the same thing as "->child[0]"; the results will behave the same in almost all
cases, but - as shown - one is falsy and the other isn't.
$branch = simplexml_load_string('<root><branch/></root>')->branch;
$branch0 =
simplexml_load_string('<root><branch/></root>')->branch[0];
echo $branch->getName(), "\n";
echo $branch->asXML(), "\n";
echo count($branch->attributes()), "\n";
echo (int)(bool)$branch, "\n";
echo $branch0->getName(), "\n";
echo $branch0->asXML(), "\n";
echo count($branch0->attributes()), "\n";
echo (int)(bool)$branch0, "\n";
echo (int)($branch[0] == $branch0);
Previous Comments:
------------------------------------------------------------------------
[2021-09-15 22:52:07] cmb@php.net
> 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.
------------------------------------------------------------------------
[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