[php-src] Issue #21583: SimpleXML: empty child element without attributes casts to true instead of false
| From: | Chi-teck | Date: | Tue, 31 Mar 2026 03:51:37 +0000 |
| Subject: | [php-src] Issue #21583: SimpleXML: empty child element without attributes casts to true instead of false | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-251871@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/21583
Author: Chi-teck
### Description
The following code:
```php
<?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
<img width="1214" height="732" alt="Image" src="https://github.com/user-attachments/assets/e383dd74-5607-44e4-bbbe-17c49cf183f3"
/>
### PHP Version
```plain
any
```
### Operating System
Debian 13