|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55538 Error when unsetting a child of an SimpleXmlElement
Submitted: 2011-08-30 11:21 UTC Modified: 2023-09-10 20:57 UTC
Avg. Score:4.0 ± 0.8
Reproduced:5 of 5 (100.0%)
Same Version:2 (40.0%)
Same OS:3 (60.0%)
From: csnaitsirch at web dot de Assigned:
Status: Verified Package: SimpleXML related
PHP Version: 5.3.8 OS: Windows 7
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: csnaitsirch at web dot de
New email:
PHP Version: OS:


 [2011-08-30 11:21 UTC] csnaitsirch at web dot de
Hi. I want to remove a child element of an SimpleXmlElement. But that produces a strange error and I think it's a bug.

This is the error message: Warning: main() [function.main]: Node no longer exists in C:\xampp\htdocs\test.php on line 5

It is very easy to reproduce. Pleas look at the test script.

Test script:
$xml = "<root><a><aa></aa></a></root>";
$xml = simplexml_load_string($xml);

foreach ($xml as $tag => $child) {

Expected result:
No errors.

Actual result:
Warning: main() [function.main]: Node no longer exists in C:\xampp\htdocs\test.php on line 5


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-30 11:26 UTC] csnaitsirch at web dot de
I forgot to mention, that the node is removed correctly. The only problem is the error message.
 [2012-10-15 10:20 UTC] opensource at prodigy7 dot de
Can confirm this behaviour. Please fix or tell us, what we do wrong.
 [2012-10-17 19:51 UTC] csnaitsirch at web dot de
Sorry, but I am not a core developer. I just made a bug report. I do not know what you made wrong. I just see that there is something wrong...
 [2015-05-12 17:28 UTC]
-Status: Open +Status: Verified
 [2015-05-12 17:28 UTC]
I'm not sure, whether this qualifies as bug in PHP. Actually,
during the iteration over a collection, removing or adding
elements can always have undesired side effects.

What's happening in this case is that after the first and only
element is deleted, the next node is requested
(php_sxe_move_forward_iterator[1]). However, this fails, because
the current element had been removed, and so GET_NODE[2] triggers
the warning.

[1] <>
[2] <>
 [2015-05-12 21:35 UTC] csnaitsirch at web dot de
Maybe one could just remove the line 'php_error_docref(NULL, E_WARNING, "Node no longer exists");', because it is just working fine. Or is there a further purpose for this warning?

HHVM does not output any warning (see
 [2015-10-14 16:07 UTC] paul at muckypuddle dot com
I think removing the warning is a valid solution here.
 [2023-09-10 20:57 UTC]
Nowadays this is an exception. I can fix that it throws an exception. However, this might still not give the expected result.
If we consider the following xml:
$xml = "<root><first/><second/><third/></root>";
Then with the fix, only <first/> will be removed. That's because unsetting removes the node from the document, so when the next iteration hits, there's no next node anymore and the iteration will stop.
It's possible to work around that, but I'm not sure what the expected behaviour would actually be.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 15 02:01:28 2024 UTC