dowaw80220 2014-10-02 16:51
浏览 38
已采纳

奇怪的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;
    }
  • 写回答

2条回答 默认 最新

  • dtujfmfs06058 2014-10-03 19:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因