php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43778 SimpleXML regression regarding empty() in 5.2.4
Submitted: 2008-01-07 17:22 UTC Modified: 2008-01-23 11:04 UTC
From: jdolecek at netBSD dot org Assigned: rrichards (profile)
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.5 OS: Windows 2000
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: jdolecek at netBSD dot org
New email:
PHP Version: OS:

 

 [2008-01-07 17:22 UTC] jdolecek at netBSD dot org
Description:
------------
It seems empty() on simplexml 'array' elements doesn't work same way in 5.2.5 as in 5.2.3. In 5.2.5, empty() returns true even through the elements are actually present. Same code run under 5.2.3 works correctly, i.e. returning true only if the element is not present.

Workaround is replace (!empty(...)) condition with isset() and test for count(), but this is inconvenient and breaks backwards compatibility.

Reproduce code:
---------------
<?php

$str = '<?xml version="1.0" encoding="WINDOWS-1250" ?>

<config>

        <items>
                <item command="overview"/>
                <item command="deals"/>
                <item command="regions"/>
                <item command="estimations"/>
                <item command="reports"/>
                <item command="import"/>
        </items>

</config>
';

$xml = simplexml_load_string($str);

echo (empty($xml->items->item) ? "EMPTY" : "full")."\n";
echo count($xml->items->item) ."\n";


Expected result:
----------------
full
6

Actual result:
--------------
EMPTY
6

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-01-07 20:13 UTC] hubert dot roksor at gmail dot com
Actually, this doesn't seem to be a regression but rather the intended behaviour, as per this commit: http://marc.info/?l=php-cvs&m=118352557820634&w=2

As per PHP's manual, empty() "[determines] whether a variable is considered to be empty". $xml->items->item will return the first "item" node, and since that node has no children and no content it is considered empty. If you only want to test whether or not an element is present, without inspecting its content, then you should use isset() instead.

Hope that makes sense to you. Note: of course, this is only my personal interpretation, nothing official.
 [2008-01-12 15:38 UTC] tony2001@php.net
Rob, please confirm it.
 [2008-01-23 11:04 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The change was intentional so that simplexml would respect empty() behavior properly.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 11:01:30 2024 UTC