dpw70180 2012-05-01 20:42
浏览 116
已采纳

使用EOF的缺点?

I've seen HTML nested within a PHP page written in so many ways... some of which are demonstrated in this page: http://austingulati.com/2010/01/many-ways-to-integrate-html-into-php/

But... I very rarely see HTML and PHP written in unison as follows:

echo <<<EOF
<h1>Welcome</h1>
<p>Hello</p>
<p>{$somePHPVariable}</p>
EOF;

Question:

Is there a fundamental issue with using the EOF approach that I should be aware of?

  • 写回答

3条回答 默认 最新

  • dongtan6336 2012-05-01 20:51
    关注

    Heredocs are wonderful if you're building HTML and need to embed variables.

    They honor the linebreaks/spacing you embed within them (even if the browser doesn't display that) so it's MUCH easier to build nicely formatted HTML, and also relieve you of the need to escape quotes while building the strings:

    e.g. compare

    print("<div class=\"this\">
    \tblah blah
    \t\t<span class=\"that\">blah</span>
    </div>");
    

    v.s.

    echo <<<EOL
    <div class="this">
        blah blah
            <span class="that"</span>
    </div>
    EOL;
    

    They can also be used in concatenation operations, eg.

    $x = "hello";
    $x .= <<<EOL
      there, how 
    EOL
    $x .= <<<EOL
      are you?
    EOL;
    

    will eventually give $x the value hello there, how are you?. Basically consider the heredoc syntax to be a VERY fancy version of double-quoted strings, with none of the drawbacks. The only restriction is that the heredoc's sentinal value must be on a line by itself, so there's no way to make a "one line" heredoc.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料