Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:22:06+00:00 2026-06-18T08:22:06+00:00

Consider the following PHP code <?php $html_data = ‘<html><body> <ol> <li><strong>Question 1</strong> Answer1</li> <li><strong>Question

  • 0

Consider the following PHP code

<?php

 $html_data = 
 '<html><body>
  <ol>
  <li><strong>Question 1</strong> Answer1</li>
  <li><strong>Question 2</strong> Answer2</li>
  </ol></body></html>';

  $doc = new DOMDocument();
  $doc->loadHTML($html_data);
  $xpath = new DOMXPath($doc);

  $ols = $xpath->query('//ol');
  $ol = $ols->item(0);
  $lis = $ol->childNodes;

  foreach ($lis as $li) {
    echo $li->firstChild->nodeValue."<br />";
    echo $li->lastChild->nodeValue."<br />";
    //echo $li->childNodes->item(0)->nodeValue."<br />";
  }
  ?>

If I remove the comment on the last line of this code and access the childNodes DOM Object Array, my foreach loop executes only once. However, if I access the same elements using firstChild and lastChild as shown above, I can successfully iterate over all the ‘li’ tags present.

I can’t make any sense of this at all. Is this a bug in PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:22:07+00:00Added an answer on June 18, 2026 at 8:22 am

    If you wouldn’t suprress your error reporting, you would have seen that you have a fatal error that breaks your script.

    In order to work with the item method:

    foreach ($lis as $li) {
      if (method_exists($li->childNodes, 'item')) {
        echo $li->childNodes->item(0)->nodeValue."<br />";
        // To reproduce the exact output you need this line also. 
        // You need to display the second child (Answer)
        echo $li->childNodes->item(1)->nodeValue."<br />";
      }  
    }
    

    The only difference it was that the first script

    foreach ($lis as $li) {
      echo $li->firstChild->nodeValue."<br />";
      echo $li->lastChild->nodeValue."<br />";    
      //echo $li->childNodes->item(0)->nodeValue."<br />";
    }
    

    Only throws Notice: Trying to get property of non-object, but the scripts continues.

    As with method item() it throws a fatal error. (Fatal error: Call to a member function item() on a non-object). which kills your script.

    For more details on how you should iterate on these nodesList (foreach vs. for) read the comments from these pages

    • http://www.php.net/manual/en/class.domnodelist.php
    • http://www.php.net/manual/en/domnodelist.item.php

    And you especially have this issue because of the trailing space after the <li> tags.

    It loops like this: first <li> tag, then the space ' ' DOMText element then the second <li> tag then the second ' ' DOMText element.

    On the DOMText element it crashes. You could clear the spaces and it would work.

    $html_data = '<html><body><ol><li><strong>Question 1</strong> Answer1</li><li><strong>Question 2</strong> Answer2</li></ol></body></html>';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following php code used for HTML output: echo <div id='logo2'> <a href='if(checkLanguage())
Consider the following code: <?php $conn = mysql_connect('localhost', 'username', 'password'); mysql_select_db('database', $conn); ?> This
consider the following code scenario: <?php //widgetfactory.class.php // define a class class WidgetFactory {
consider the following PHP code: <?php $searchsport = $_REQUEST['sport']; $sportarray = array( Football =>
Consider the following PHP class code class SuperIdea{ . . . static function getById($id){
Consider the following code <?php $username = root; $password = ; $host = localhost;
Consider the following HTML5 code in body: <p id=p1> Email: <input type=text name=email /><br
Consider the following rules RewriteRule ^index\.html$ index.php [L] RewriteRule ^index\.php$ index.html [R=301] I want
Consider a document in the following format: <!DOCTYPE html> <html> <head> <title></title> <body> <div
Consider the following code <?php // warning: this code is unsafe and for demonstrational

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.