php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81443 Inconsistent casting to bool of SimpleXML objects
Submitted: 2021-09-15 21:42 UTC Modified: 2021-09-15 22:52 UTC
From: rk at srsbiz dot pl Assigned: cmb (profile)
Status: Not a bug Package: SimpleXML related
PHP Version: Irrelevant OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rk at srsbiz dot pl
New email:
PHP Version: OS:

 

 [2021-09-15 21:42 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-15 22:52 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2021-09-15 22:52 UTC] 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-16 00:25 UTC] a at b dot c dot de
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);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 00:01:30 2024 UTC