diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c
index 8cf8e657e58f..365ae4518e2c 100644
--- a/ext/simplexml/simplexml.c
+++ b/ext/simplexml/simplexml.c
@@ -1843,12 +1843,7 @@ static zend_result sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int
sxe = php_sxe_fetch_object(readobj);
if (type == _IS_BOOL) {
- node = php_sxe_get_first_node_non_destructive(sxe, NULL);
- if (node) {
- ZVAL_TRUE(writeobj);
- } else {
- ZVAL_BOOL(writeobj, !sxe_prop_is_empty(readobj));
- }
+ ZVAL_BOOL(writeobj, !sxe_prop_is_empty(readobj));
return SUCCESS;
}
diff --git a/ext/simplexml/tests/000.phpt b/ext/simplexml/tests/000.phpt
index d640d0652f4b..acfeaea9f02d 100644
--- a/ext/simplexml/tests/000.phpt
+++ b/ext/simplexml/tests/000.phpt
@@ -221,7 +221,7 @@ object(SimpleXMLElement)#%d (1) {
}
===sxe->elem11->elem111->elem1111
bool(true)
-bool(true)
+bool(false)
int(1)
object(SimpleXMLElement)#%d (0) {
}
diff --git a/ext/simplexml/tests/gh21583.phpt b/ext/simplexml/tests/gh21583.phpt
new file mode 100644
index 000000000000..5ea33697f041
--- /dev/null
+++ b/ext/simplexml/tests/gh21583.phpt
@@ -0,0 +1,18 @@
+--TEST--
+GH-21583 (SimpleXML empty element incorrectly casts to true)
+--EXTENSIONS--
+simplexml
+--FILE--
+text');
+
+var_dump((bool) $xml->bar);
+var_dump((bool) $xml->baz);
+var_dump((bool) $xml->qux);
+var_dump((bool) $xml->nonexistent);
+?>
+--EXPECT--
+bool(false)
+bool(true)
+bool(true)
+bool(false)