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) もブラウザーに表示されます。上記のようにブラウザに表示されます。

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

私が試したコメント境界の順列:

// <--  
// -->

と....

<-- //
--> //

どちらの場合も FAIL で、内部でコメントできるようにしますheredoc

では、どうすれば s 内のコードをコメントアップできheredocますか?

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

それは設計によるものです。ヒアドキュメントになると、入力するすべてが終了するまで、1 つの長い文字列の一部として扱われます。あなたの最善の策は、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 に答える