Recursive comment function echoes HTML inside function To display comments recursively I'm using this:

function fetch_article_comments($article_id, $parent_id) {
$app = new Connection();

    if ($parent_id &lt;= 0) {
        $parent_id = NULL;
    }
    $sql = "SELECT * FROM recursive WHERE article_id = :article_id AND comment_parent &lt;=&gt; :parent_id ORDER BY comment_timestamp ASC";
    $query = $app-&gt;getConnection()-&gt;prepare($sql);

    $query-&gt;execute(array(':article_id' =&gt; $article_id,
                          ':parent_id' =&gt; $parent_id));

    return $query-&gt;fetchAll();
}


And to display the comments:

function display_comments ($article_id, $parent_id=0, $level=0) {
$comments = fetch_article_comments($article_id, $parent_id);
  foreach($comments as $comment) {
        $comment_id = $comment-&gt;comment_id;   
        $member_id = $comment-&gt;member_id;
        $comment_text = $comment-&gt;comment_text;
        $comment_timestamp = $comment-&gt;comment_timestamp;  //get timeAgo


        // RENDER
      ?&gt;
          &lt;div class="comment"&gt;
              &lt;b&gt;&lt;?= timeAgoInWords($comment_timestamp); ?&gt;&lt;/b&gt;&lt;br&gt;
          &lt;?= $comment_text ?&gt;&lt;br&gt;
              &lt;a href="#" class="replyLink" data-comment-id="&lt;?= $comment_id ?&gt;"&gt;&lt;span class="uk-icon-comment-o"&gt; reply&lt;/span&gt;&lt;/a&gt;
              &lt;a href="recursive.php?delete=&lt;?= $comment_id ?&gt;"&gt;&lt;span class="uk-icon-trash-o"&gt; delete&lt;/span&gt;&lt;/a&gt;


    &lt;form action="recursive.php" method="post" class="uk-form comment_form" id="&lt;?= $comment_id ?&gt;"&gt;
    &lt;fieldset data-uk-margin&gt;
        &lt;input type="hidden" name="id" value="&lt;?= $article_id ?&gt;" /&gt;
        &lt;input type="hidden" name="parent" value="&lt;?= $comment_id ?&gt;" /&gt;
        &lt;input type="text" placeholder="Comment" name="svar"&gt;
        &lt;button class="uk-button"&gt;Comment&lt;/button&gt;
    &lt;/fieldset&gt;
&lt;/form&gt;

&lt;?php
        //Recurse
        display_comments($article_id, $comment_id, $level+1);
                echo '&lt;/div&gt; &lt;!-- /comment --&gt;';
    }

}
?&gt;


And the comments is then echoed on the page calling the display_comments().

        &lt;div clasS="uk-width-1-1"&gt;
&lt;?= display_comments(2); ?&gt;
        &lt;/div&gt;


(the 2 is the post ID)

As you can see there's a lot of HTML inside the function, it's not ideal. How can the code be improved to remove the HTML and maybe foreach the results?
I would split up the function into 2 parts


fetch comments and recurse
render html for comment


To me it is easier to see the looping logic without all the html in place

You could then also look at templating engine if you are going to be doing lots of this sort of thing, there are various options available, smarty, twig, mustache, blades to name a few.

function display_comments($article_id, $parent_id = 0, $level = 0)
{
    // TODO handle sql connection error
    $comments = fetch_article_comments($article_id, $parent_id);

    foreach ($comments as $comment) {
        display_comment_html($comment);

        display_comments($article_id, $comment-&gt;comment_id, $level + 1);
        echo '&lt;/div&gt; &lt;!-- /comment --&gt;';
    }

}


function display_comment_html($comment)
{
    $comment_id = $comment-&gt;comment_id;
    $member_id = $comment-&gt;member_id;
    $comment_text = $comment-&gt;comment_text;
    $comment_timestamp = $comment-&gt;comment_timestamp;  //get timeAgo


// RENDER
    ?&gt;
    &lt;div class="comment"&gt;
    &lt;b&gt;&lt;?= timeAgoInWords($comment_timestamp); ?&gt;&lt;/b&gt;&lt;br&gt;
    &lt;?= $comment_text ?&gt;&lt;br&gt;
    &lt;a href="#" class="replyLink" data-comment-id="&lt;?= $comment_id ?&gt;"&gt;&lt;span class="uk-icon-comment-o"&gt; reply&lt;/span&gt;&lt;/a&gt;
    &lt;a href="recursive.php?delete=&lt;?= $comment_id ?&gt;"&gt;&lt;span class="uk-icon-trash-o"&gt; delete&lt;/span&gt;&lt;/a&gt;


    &lt;form action="recursive.php" method="post" class="uk-form comment_form" id="&lt;?= $comment_id ?&gt;"&gt;
        &lt;fieldset data-uk-margin&gt;
            &lt;input type="hidden" name="id" value="&lt;?= $article_id ?&gt;"/&gt;
            &lt;input type="hidden" name="parent" value="&lt;?= $comment_id ?&gt;"/&gt;
            &lt;input type="text" placeholder="Comment" name="svar"&gt;
            &lt;button class="uk-button"&gt;Comment&lt;/button&gt;
        &lt;/fieldset&gt;
    &lt;/form&gt;
    &lt;?php
}