SimpleXML: empty child element without attributes casts to true instead of false
Open
@devnexen is already working on this.
Since Mar 31, 2026.
- #21590 by @devnexen — closed without merging
Bug
Extension: simplexml
Status: Verified
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
The following code:
<?php
$cases = [
'<foo><bar>content</bar></foo>',
'<foo><bar/></foo>',
'<foo/>',
];
foreach ($cases as $case) {
\printf("%-30s: %s\n", $case, new \SimpleXMLElement($case)->bar ? '✅' : '❌');
}
Resulted in this output:
<foo><bar>content</bar></foo> : ✅
<foo><bar/></foo> : ✅
<foo/> : ❌
But I expected this output instead:
<foo><bar>content</bar></foo> : ✅
<foo><bar/></foo> : ❌
<foo/> : ❌
The documentation explicitly lists empty SimpleXML elements without attributes as falsy.
https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
PHP Version
any
Operating System
Debian 13
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.