奇怪的HEREDOC输出顺序

I am using (or at least tying to) PHP HEREDOC function as a templating engine. I have implemented external caller string that can directly process external functions in HEREDOC, and that works successfully.

The problem I am facing now is that the order of certain functions appear to take precedence and execute first, regardless of other functions and/or code inside the specific HEREDOC.

How to fix that?

(Please note I am a PHP beginner. I have done my homework, but couldn't find a solution. Thanks.)

FUNCTION PROCESOR:

function heredoc($input)
    {
    return $input;
    }
    $heredoc = "heredoc";

HEREDOC TEMPLATE:

function splicemaster_return_full_page()
    {
    global $heredoc;
    $title ="This is document title";
    echo <<<HEREDOC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{$heredoc(splice_html_title($title))}
</head>
<body>
{$heredoc(splicemaster_return_message())}
{$heredoc(splice_quick_add_article_form())}
{$heredoc(display_all_articles_in_a_html_table())}
</body>
</html>
HEREDOC;
    }

The issue at hand is with "{$heredoc(display_all_articles_in_a_html_table())}" call, which outputs before everything else, resulting in a broken HTML.

Any help appreciated, I am banging my head with this for quite a while now.

UPDATE:

using stuff posted in comments i tried to do something else, but this is ugly as hell, and I would have issues editing this at later date.

function testout()
    {
    $title = "This is document title";

echo "<!DOCTYPE html>";
echo '<html lang="en">';
echo     "<head>";
echo       '<meta charset="utf-8">';
echo         "<title>". $title . "</title>";
echo     "</head>";
echo     "<body>";
echo splicemaster_return_message();
echo splice_quick_add_article_form();
echo display_all_articles_in_a_html_table();
echo     "</body>";
echo "</html>";

    }

(How it looks like is not important - I have a HTML processor function.)

UPDATE 2

OK, so I found "dirty" fix, tho that doesn't explain why the engine works as it does. (I also tested on another machine, with diff. php):

function splicemaster_return_full_page()
    {
    global $heredoc;
    $title ="This is document title";

    echo <<<HEREDOC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{$heredoc(splice_html_title($title))}
</head>
<body>
{$heredoc(splicemaster_return_message())}
{$heredoc(splice_quick_add_article_form())}
HEREDOC;
    echo <<<HEREDOC
{$heredoc(display_all_articles_in_a_html_table())}
</body>
</html>
HEREDOC;
    }

I asked this (similar) question on other site while seeking why this happens, and found the culprit.

The problem was in called functions that echo (or print) output, instead returning it. When I switched to return, the code outputs appropriately.

You shouldn't be using heredoc here. Or really be trying to render an entire html document within a function. This is how html should be rendered with php. Note: I'm also pretty sure you can't call functions in a heredoc statement.

<?php $title = "This is document title"; ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <?php echo splice_html_title($title); ?>
    </head>
    <body>
        <?php
            echo splicemaster_return_message()
               . splice_quick_add_article_form()
               . display_all_articles_in_a_html_table();
        ?>
    </body>
</html>

You can see how much cleaner this is, which makes it much easier to edit, when needed. You just put this in a file 'page.php' for example.

include_once('page.php');

And include it where ever you would call that function splicemaster_return_full_page.