Recursive comment function echoes HTML inside function
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
}