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条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭