php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34199 SimpleXMLElement is Schroedinger's Cat
Submitted: 2005-08-20 21:13 UTC Modified: 2005-09-15 16:34 UTC
Votes:3
Avg. Score:3.3 ± 1.7
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: rasmus@php.net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5CVS-2005-08-20 (CVS) OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rasmus@php.net
New email:
PHP Version: OS:

 

 [2005-08-20 21:13 UTC] rasmus@php.net
Description:
------------
An empty SimpleXMLElement object is both true and false

Reproduce code:
---------------
$xml = "<root></root>";
$xml = simplexml_load_string($xml);
$kids = $xml->children();
if($kids) echo "True";
if(!$kids) echo "False";

Expected result:
----------------
False

Actual result:
--------------
TrueFalse

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-08-20 22:33 UTC] rrichards@php.net
if ($kids) if handled by ZEND_JMPZ_SPEC_CV_HANDLER which ends up calling i_zend_is_true and returning TRUE because $kids is an object.

if (! $kids) is handled by ZEND_BOOL_NOT_SPEC_CV_HANDLER which calls convert_to_boolean and ends up calling the objects cast_object handler, which in this case is sxe_object_cast and returns FALSE because object is really empty.
 [2005-08-20 22:53 UTC] rasmus@php.net
Marking it critical.  I think we need to get this cleaned up before 5.1 goes out.  Having 2 different opcodes handling this that can potentially return conflicting results causes all sorts of confusion.  Took me about an hour working backwards through assumptions before I finally figured out that if() and if(!) were to blame.
 [2005-08-23 03:56 UTC] sniper@php.net
This outputs nothing with PHP 5, but does give "yes" with 4.4:

<?php
$foo = new StdClass();
if(!$foo) echo "empty object: yes";
?>

So in PHP5, an "empty" object is still true.
(then again, empty array() is considered false)
Was the change intentional? And if so, why?

 [2005-09-12 13:54 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_1.

Now we call cast handler in both situations.

According to Jani's question - yes.
In PHP5, an "empty" object is true.
However you can use "zend.ze1_compatibility_mode".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC