<?php
$cases = [
'<foo><bar>content</bar></foo>',
'<foo><bar/></foo>',
'<foo/>',
];
foreach ($cases as $case) {
\printf("%-30s: %s\n", $case, new \SimpleXMLElement($case)->bar ? '✅' : '❌');
}
<foo><bar>content</bar></foo> : ✅
<foo><bar/></foo> : ✅
<foo/> : ❌
<foo><bar>content</bar></foo> : ✅
<foo><bar/></foo> : ❌
<foo/> : ❌
The following code:
Resulted in this output:
But I expected this output instead:
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
Operating System
Debian 13