php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35899 Empty objects returned from simplexml_load_string evaluate to false.
Submitted: 2006-01-05 02:40 UTC Modified: 2006-01-05 14:00 UTC
From: si at tinypla dot net Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.1.1 OS: RHEL3
Private report: No CVE-ID: None
 [2006-01-05 02:40 UTC] si at tinypla dot net
Description:
------------
Hi All,
   Firstly, this covers ground in <a href="http://bugs.php.net/bug.php?id=35028">bug #35028</a> and is related, but more specific. 

php 5.1.1 appears to interpret standard empty objects as "true" (as did 5.0.x). 
Unlike 5.0.x (and StdClass() in 5.1.1) empty objects returned from the simpleXML parser in 5.1.1 appear to be evaluated as "false" when queried in an if() statement.  

I know we should all be using isset(), but not all programmers play by these rules, and code which was made based on 5.0.x's way of doing it doesn't work. If I try and include this code in my app, it breaks. It could be argued that this logic is counter intuitive.

Reproduce code:
---------------
function runTest($object){
  if($object) { 
    echo "php 5.1.1 evaluates object as true\n";
  } else {
    echo "php 5.1.1 evaluates object as false\n";
  }
  var_dump($object);
}

$object0 = new StdClass();
$object1 = simplexml_load_string('<foo attr="value">bar</foo>');
$object2 = simplexml_load_string('<foo attr="value"></foo>');
$object3 = simplexml_load_string('<foo attr="value" />');

runTest($object0);
runTest($object1);
runTest($object2);
runTest($object3);


Expected result:
----------------
php 5.1.1 evaluates object as true
object(stdClass)#1 (0) {
}

php 5.1.1 evaluates object as true
object(SimpleXMLElement)#2 (1) {
  [0]=>
  string(3) "bar"
}

php 5.1.1 evaluates object as true
object(SimpleXMLElement)#2 (0) {
}

php 5.1.1 evaluates object as true
object(SimpleXMLElement)#2 (0) {
}

Actual result:
--------------
php 5.1.1 evaluates object as true
object(stdClass)#1 (0) {
}

php 5.1.1 evaluates object as true
object(SimpleXMLElement)#2 (1) {
  [0]=>
  string(3) "bar"
}

php 5.1.1 evaluates object as false
object(SimpleXMLElement)#2 (0) {
}

php 5.1.1 evaluates object as false
object(SimpleXMLElement)#2 (0) {
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-05 10:08 UTC] sniper@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.


 [2006-01-05 14:00 UTC] si at tinypla dot net
I hope the test helps someone out who is head scatching, 
Thank you for your reply, at least I know now that I am 
not going insane...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 15 04:01:30 2024 UTC