php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #56239 count for SimpleXml nodes return wrong value
Submitted: 2004-11-29 23:46 UTC Modified: 2013-07-31 05:22 UTC
From: tim dot lokot at printsoft dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5CVS-2004-11-29 (dev) OS: XP
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: tim dot lokot at printsoft dot com
New email:
PHP Version: OS:

 

 [2004-11-29 23:46 UTC] tim dot lokot at printsoft dot com
Description:
------------
PHP is 5.0.2 firstly, it wasn't in the list, so I just chose the cvs version.

I am trying to count how many nodes are under another node in an xml document by counting them with count().  For some reason, even when I have more than one node, it always returns 1.  Same with the children() function.

Basically in my code, I'm trying to prove that Order->OrderLines['NumLines'] == count(Order->OrderLines->OrderLine)

Reproduce code:
---------------
XML Fragment:

  <Order>
    <OrderLines NumLines="2">
      <OrderLine Number="1" StampID="435" Quantity="2" />
      <OrderLine Number="2" StampID="733" Quantity="5" />
    </OrderLines>
  </Order>


PHP Code;

$order = $xml->Order; // set previously

  count($order->OrderLines->children()))

    or

  count($order->OrderLines->OrderLine)

Expected result:
----------------
2

Actual result:
--------------
1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-31 05:22 UTC] yohgaki@php.net
-Status: Open +Status: Not a bug -Package: domxml +Package: *General Issues
 [2013-07-31 05:22 UTC] yohgaki@php.net
Usage is wrong. Please read 
http://jp1.php.net/manual/en/book.simplexml.php

In simplexml, root node is assumed always exists and represented as var. So you 
cannot refer to it.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 15:01:28 2025 UTC