Ask a Question related to PHP Bugs, Design and Development.
-
iliaa@php.net #1
#37817 [Asn->Bgs]: bad foreach loop the second time with simple xml
ID: 37817
Updated by: [email]iliaa@php.net[/email]
Reported By: ecervetti at ch-hyeres dot fr
-Status: Assigned
+Status: Bogus
Bug Type: SimpleXML related
Operating System: Linux mandriva 2006
PHP Version: 5.1.4
Assigned To: helly
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
[url]http://www.php.net/manual/[/url] and the instructions on how to report
a bug at [url]http://bugs.php.net/how-to-report.php[/url]
When you access an non-existenet element SimpleXML creates a
node in the DOM tree. This node is then present for future
iterations of the DOM tree.
To avoid creating the DOM node, use the isset($xml->typebal3)
code to check for its presence. This will allow you to
determine if the node exists and not create if it does not.
Previous Comments:
------------------------------------------------------------------------
[2006-08-08 17:09:20] ale at estudionemo dot com
I've found a post in the mailing list about this issue:
[url]http://www.mail-archive.com/php-general@lists.php.net/msg190494.html[/url]
It says that "it's not a bug, it's a feature".
It behaves in the same way in PHP 5.1.4 under Solaris 10 and Ubuntu
6.06.
------------------------------------------------------------------------
[2006-06-19 08:02:57] [email]mike@php.net[/email]
Verified with 5.2
------------------------------------------------------------------------
[2006-06-15 12:24:42] ecervetti at ch-hyeres dot fr
Description:
------------
I try to access a particular element of xml data with the
foreach($xml->name_of_element ....) syntax.
But I don't know if this element exists.
When the element don't exists, no problem the first time;
It don't goes inside the loop.
But If I call the same loop a second time, it goes inside the loop, as
if the element exists.
Reproduce code:
---------------
$string =
"<data><typebal1>pim</typebal1><typebal2>poom</typebal2></data>";
$xml = simplexml_load_string($string);
foreach($xml->typebal3 as $bal) {
print "<br />I should not be here<br />";
}
foreach($xml->typebal3 as $bal) {
print "<br />I should either not be here<br />";
}
//this script renders "I should either not be here"
Expected result:
----------------
this script should not print anything
Actual result:
--------------
this script renders "I should either not be here"
------------------------------------------------------------------------
--
Edit this bug report at [url]http://bugs.php.net/?id=37817&edit=1[/url]
iliaa@php.net Guest
-
foreach/while loop
I'm trying to learn php (and mysql) and I've apparently done something wrong. If I use the line: while ($row=mysql_fetch_array($res,... -
Help requird : got a problem with foreach loop???
Hi everybody, I have a small query with foreach loop( as well as while loop). Here is the small script with which i got a problem ... -
How to retrieve a scalar name in a foreach loop
I have a question that's really bugging me. I've tried looking in the Perl FAQ, the Perl Cookbook, the White Camel, and the Perl How-to under words... -
[PHP] error on array loop through foreach?
Assuming line 43 is the line "foreach ($filename as $filevalue){", one guess would be that the variable $filename is not an array; an array is... -
Naming some arrays in a foreach loop?!
Hey guys, I've to read out some rows of some mysql db. No problem at all. But I've to seperate the results for different machines. So I tried...



Reply With Quote

