需要将代码注释放在heredoc中

wan*_*est 8 php comments heredoc

好吧,我似乎无法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;
Run Code Online (Sandbox Code Playgroud)

该表单是否有效,确定(顺便提一下上面的表单代码是为了我的问题而高度截断).

但dang comment(okay this comment was..blah blah blah)也出现在浏览器中.它在浏览器中显示如上所示:

// okay this comment was intended to explain the code below but it
// is showing up on the web page HTML sent to the browser
Run Code Online (Sandbox Code Playgroud)

关于评论划分的排列我尝试过:

// <--  
// -->
Run Code Online (Sandbox Code Playgroud)

和....

<-- //
--> //
Run Code Online (Sandbox Code Playgroud)

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

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

DaO*_*gre 9

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

echo <<<_HEREDOC_FOO
    text text text
<<<_HEREDOC_FOO;
//Comments
echo <<<_HEREDOC_FOO
    text text text
<<<_HEREDOC_FOO;
Run Code Online (Sandbox Code Playgroud)

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


小智 9

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

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;
Run Code Online (Sandbox Code Playgroud)

  • 可能不值得这么麻烦,但仍然很棒。 (2认同)