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

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug