title: php DOMDocument nodeName property returning '#text' with the nodeName
body: <p>I want to extract the content of body of a html page along with the tagNames of its child. I have taken an example html like this:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
  &lt;h1&gt;This is H1 tag&lt;/h1&gt;
  &lt;h2&gt;This is H2 tag&lt;/h2&gt;
  &lt;h3&gt;This is H3 tag&lt;/h3&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I have implemented the php code like below and its working fine.</p>
  
  <pre><code>$d=new DOMDocument();
  $d-&gt;loadHTMLFile('file.html');
  $l=$d-&gt;childNodes-&gt;item(1)-&gt;childNodes-&gt;item(1)-&gt;childNodes;
  for($i=0;$i&lt;$l-&gt;length;$i++)
  {
  echo "&lt;".$l-&gt;item($i)-&gt;nodeName."&gt;".$l-&gt;item($i)-&gt;nodeValue."&lt;/".$l-&gt;item($i)-&gt;nodeName."&gt;";
  }
  </code></pre>
  
  <p>This code is working perfectly fine, but when I tried to do this using foreach loop instead of for loop, the nodeName property was returning '#text' with every actual nodeName. 
  Here is that code</p>
  
  <pre><code>$l=$d-&gt;childNodes-&gt;item(1)-&gt;childNodes-&gt;item(1)-&gt;childNodes;
  foreach ($l as $li) {
      echo $li-&gt;childNodes-&gt;item(0)-&gt;nodeName."&lt;br/&gt;";
  }
  </code></pre>
  
  <p>Why so?</p>