需要将代码注释放入定界文档中

发布于 2024-11-01 19:16:21 字数 929 浏览 1 评论 0原文

好吧,我似乎无法在 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;

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

但是该死的评论(好吧,这个评论是..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

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

// <--  
// -->

和...

<-- //
--> //

在这两种情况下都失败,无法允许我在 heredoc 内发表评论。

那么我到底怎样才能在我的 heredoc 中注释我的代码呢?

Well I can't seem to add comments inside a heredoc block in my foo.php file:

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. 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.

So how the heck can I comment up my code within my heredocs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮倦 2024-11-08 19:16:21

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

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

正如其他人提到的,你可以做 HTML 评论,但那些查看你的源代码的人仍然可以看到

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. Your best bet would be to break your HEREDOC, put your comment, then start a new echo line

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

城歌 2024-11-08 19:16:21

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

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;

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;
咽泪装欢 2024-11-08 19:16:21

试试这个:

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 评论

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

木緿 2024-11-08 19:16:21

实际执行此操作的最简单方法是使用与 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中插入注释方式。如果您不在全局范围内定义该函数,这也将起作用。

The simplest way to actually do this is using the same tactic as SeppoTaalasmaa used, but then shorter:

$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. This will also work if you're not defining the function in the global scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文