简体   繁体   English

需要将代码注释放在heredoc中

[英]need to put code comments inside a heredoc

Well I can't seem to add comments inside a heredoc block in my foo.php file: 好吧,我似乎无法在我的foo.php文件中的heredoc块中添加注释:

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;

Does the form work, sure (btw the form code above is highly truncated for the sake of my question here). 该表单是否有效,确定(顺便提一下上面的表单代码是为了我的问题而高度截断)。

But the dang comment ( okay this comment was..blah blah blah ) appears in the browser too. 但是dang的评论( okay this comment was..blah blah blah )也出现在浏览器中。 It shows up in the browser just as written above: 它在浏览器中显示如上所示:

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

Permutations on the commenting demarcation I've tried: 关于评论划分的排列我尝试过:

// <--  
// -->

and.... 和....

<-- //
--> //

FAIL in both cases to allow me to comment inside heredoc . 在这两种情况下失败都允许我在heredoc发表评论。

So how the heck can I comment up my code within my heredoc s? 那么我怎么能在我的heredoc s中评论我的代码呢?

You could pass the comment string as a parameter of a variable function. 您可以将注释字符串作为变量函数的参数传递。

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;

That's by design. 这是设计的。 Once you being your heredoc EVERYTHING you type until you end it is treated as being part of one long string. 一旦你成为你的heredoc一切你输入直到你结束它被视为一个长字符串的一部分。 Your best bet would be to break your HEREDOC, put your comment, then start a new echo line 你最好的选择是打破你的HEREDOC,发表你的评论,然后开始一个新的回声线

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

As someone else mentioned you could do HTML comments, but those will still be visible to anyone who views your source code 正如其他人提到的那样,您可以进行HTML注释,但查看源代码的任何人都可以看到这些注释

Try this: 尝试这个:

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;

it is now an HTML comment 它现在是一个HTML评论

The simplest way to actually do this is using the same tactic as SeppoTaalasmaa used, but then shorter: 实际执行此操作的最简单方法是使用与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;

Just add the first line defining $comment , and you'll be able to insert comments in the next heredoc this way. 只需添加定义$comment的第一行,您就可以通过这种方式在下一个heredoc中插入注释。 This will also work if you're not defining the function in the global scope. 如果您未在全局范围内定义函数,这也将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM