Skip to content

Regression: SimpleXMLElement (bool) cast — empty/"0" elements false; empty($missingVar) false (ext/simplexml/sxe.c) #22714

Description

@PurHur

Category

stdlib · php-src-strict · SimpleXML scalar cast (pillar 4 / live-node adjacent)

Problem

Zend sxe_object_cast_ex treats a present element as boolean true even when its text is empty or "0". Only a missing/blank node property is false. After $z = $xml->missing, empty($z) is true on Zend.

VM (bool) follows ordinary string-truthiness of the text content (empty/"0" → false), and empty($z) after assigning a missing child is false.

Distinct from #19707 (empty($xml->child) property-fetch path — already green for empty($xml->z)).

Probed 2026-07-23 on master VM vs host Zend 8.2.32 (./script/docker-exec.sh). Use parentheses: (bool)($xml->a) (cast binds tighter than ->).

Repro ($xml = simplexml_load_string('<r><a/><b>0</b><d>x</d></r>')) Zend 8.2+ VM (2026-07-23)
(bool)($xml->a) empty element true false
(bool)($xml->b) text "0" true false
(bool)($xml->d) text "x" true true
(bool)($xml->z) missing false false
$z=$xml->z; empty($z) true false

php-src reference

  • php/php-src ext/simplexml/simplexml.c — sxe_object_cast_ex (IS_FALSE / IS_TRUE / _IS_BOOL)
  • Missing/blank node detection used by cast and empty() on an already-fetched SimpleXMLElement

PHP implementation target

  • lib/VM/Variable.php (toBool) and/or ext/simplexml/VmSimpleXml.php — SimpleXMLElement object cast to bool per sxe_object_cast_ex (present element ⇒ true; blank/missing ⇒ false)
  • Ensure empty($sxeVar) on an assigned missing-child view matches Zend (related blank-node check)
  • JIT/AOT object-cast lowering must share the same rule; no new runtime/*.c logic

Repro

./script/docker-exec.sh -- bash -lc 'cat > /tmp/sxe_bool.php <<'\''PHP'\''
<?php
$xml=simplexml_load_string("<r><a/><b>0</b></r>");
var_export((bool)($xml->a)); echo "\n";
var_export((bool)($xml->b)); echo "\n";
$z=$xml->z; var_export(empty($z)); echo "\n";
PHP
php /tmp/sxe_bool.php; php bin/vm.php /tmp/sxe_bool.php'

Done when

  • Table rows match Zend on VM (and JIT/AOT if in scope)
  • Compliance .phpt under test/compliance/cases/simplexml/
  • php-src-strict; no php-compiler-strict shortcut

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machinebugSomething isn't workingimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web appsstdlib

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions