11

好吧,我似乎无法heredoc在文件的块内添加注释foo.php

echo <<<_HEREDOC_FOO

       // okay this comment was intended to explain the code below but it
       // is showing up on the web page HTML sent to the browser

      <form action="foo.php" method="post">
      <input type="submit" value="DELETE RECORD" /></form>

_HEREDOC_FOO;

表格是否有效(顺便说一句,为了我的问题,上面的表格代码被高度截断)。

okay this comment was..blah blah blah但是浏览器中也出现了 dang 注释 ( )。它显示在浏览器中,就像上面写的一样:

// okay this comment was intended to explain the code below but it
// is showing up on the web page HTML sent to the browser

我尝试过的评论分界的排列:

// <--  
// -->

和....

<-- //
--> //

在这两种情况下都不允许我在里面发表评论heredoc

那么我怎么能在我heredoc的 s 中评论我的代码呢?

4

4 回答 4

11

您可以将注释字符串作为变量函数的参数传递。

function heredocComment($comment)
{
    return "";
}

$GLOBALS["heredocComment"] = "heredocComment";

echo <<<_HEREDOC_FOO

   {$heredocComment("
   okay this comment was intended to explain the code below but it
   is showing up on the web page html sent to the browser
   ")}

  <form action="foo.php" method="post">
  <input type="submit" value="DELETE RECORD" /></form>

_HEREDOC_FOO;
于 2012-05-08T13:26:03.197 回答
10

这是设计使然。一旦你成为你的heredoc,你输入的所有内容直到你结束它都被视为一个长字符串的一部分。你最好的选择是打破你的HEREDOC,发表你的评论,然后开始一个新的回声线

echo <<<_HEREDOC_FOO
    text text text
<<<_HEREDOC_FOO;
//Comments
echo <<<_HEREDOC_FOO
    text text text
<<<_HEREDOC_FOO;

正如其他人提到的那样,您可以进行 HTML 注释,但是任何查看您的源代码的人仍然可以看到这些注释

于 2011-04-15T16:43:29.270 回答
2

试试这个:

echo <<<_HEREDOC_FOO

       <!-- okay this comment was intended to explain the code below but it
            is showing up on the web page html sent to the browser -->

      <form action="foo.php" method="post">
      <input type="submit" value="DELETE RECORD" /></form>

_HEREDOC_FOO;

现在是HTML 注释

于 2011-04-15T16:41:19.110 回答
0

实际执行此操作的最简单方法是使用与 SeppoTaalasmaa 相同的策略,但更短:

$comment = function($str) {return '';};
echo <<<_HEREDOC_FOO

       {$comment('okay this comment was intended to explain the code below but it
       is showing up on the web page html sent to the browser')}

      <form action="foo.php" method="post">
      <input type="submit" value="DELETE RECORD" /></form>

_HEREDOC_FOO;

只需添加第一行定义$comment,您就可以通过这种方式在下一个 heredoc 中插入注释。如果您没有在全局范围内定义函数,这也将起作用。

于 2014-01-06T21:40:41.897 回答