php / php/php-src

SimpleXML: empty child element without attributes casts to true instead of false

Open
#21,583 0 comments 0 reactions 1 assignee View on GitHub

@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

Image
PHP Version
any
Operating System

Debian 13

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.