php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55773 foreach issues warning with function main()
Submitted: 2011-09-24 01:32 UTC Modified: 2011-09-24 21:01 UTC
From: Ray dot Paseur at Gmail dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.3.8 OS: Linux
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: Ray dot Paseur at Gmail dot com
New email:
PHP Version: OS:

 

 [2011-09-24 01:32 UTC] Ray dot Paseur at Gmail dot com
Description:
------------
http://www.laprbass.com/RAY_xml_delete_nodes.php
Outputs:

Warning:  main() [function.main]: Node no longer exists in /home/websitet/public_html/RAY_xml_delete_nodes.php on line 12

<?xml version="1.0"?>
<category id="1" title="category 1">
  
  <item id="b" title="item b" delete="false"/>
</category>

Test script:
---------------
<?php // RAY_xml_delete_nodes.php
error_reporting(E_ALL);
echo "<pre>";
$xml = <<<XML
<category id='1' title='category 1'>
  <item id='a' title='item a' delete='true' />
  <item id='b' title='item b' delete='false' />
</category>
XML;
$obj = SimpleXML_Load_string($xml);
$key = 0;
foreach ($obj as $item)
{
    if ($item["delete"] == 'true') unset($obj->item[$key]);
    $key++;
}

$new = $obj->asXML();
echo htmlentities($new);

Expected result:
----------------
No warning message.  The URL is here.
http://www.laprbass.com/RAY_xml_delete_nodes.php


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-24 19:03 UTC] aharvey@php.net
-Status: Open +Status: Bogus -Package: Class/Object related +Package: SimpleXML related
 [2011-09-24 19:03 UTC] aharvey@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

You're removing a child from the object whilst in the middle of
iterating over it. At that point, all bets are off, really. I'm happy
enough to call that expected behaviour, because I don't think there's
actually anything sensible that can be done.
 [2011-09-24 21:01 UTC] Ray dot Paseur at Gmail dot com
Thanks for your comment.  I have two questions before I abandon this line of thinking.  First, what would you recommend to accomplish the removal of a child from an object based on an attribute of the child?  And second, why would a warning pointing to the nonexistent function main() in the line number of foreach() be considered "expected" behavior?  The php.net web site tells us to report a bug if we get a message identifying function main().  You can look it up. 
http://php.net/manual/en/function.main.php

Surely if this is an error in foreach() an error message might be crafted that gave a hint about the problem, eh?

Feel free to contact me offline if you have questions about what I posted in the report.  I'll be glad to try to clarify or help in any way.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC