|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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) PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 16:00:01 2025 UTC |
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);