15

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?

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
wantTheBest
  • 1,682
  • 4
  • 43
  • 69
  • The short answer is you can't. What @Neal shows is the only way (but note that the comments will show up in the user's browser in the source code) – Pekka Apr 15 '11 at 16:42
  • @Pekka yes it does show, but the user does not see it unless they view source – Naftali Apr 15 '11 at 16:43

4 Answers4

14

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;
SeppoTaalasmaa
  • 141
  • 1
  • 2
14

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

DaOgre
  • 2,080
  • 16
  • 25
  • this defeats the purpose of the HEREDOC – Naftali Apr 15 '11 at 16:44
  • 1
    Well he mentioned his text is massively truncated, and if he has server side comments he doesn't want shown, this is the only way to do it. Your mentioned method is correct, but only if it's ok for HTML comments to be included – DaOgre Apr 15 '11 at 16:48
  • @DaoOgre as i explained to the downvoter of my answer, the OP never specified what type of comment the OP wants – Naftali Apr 15 '11 at 16:50
  • Actually (in my particular case anyway) the comments should not be visible in source viewable by the browser user when they view source, and I will break the heredoc to make it work -- thanks all, and as you have probably suspected I'm new to php etc. (lots of programming knowledge but new to php, etc.) – wantTheBest Apr 15 '11 at 17:05
  • No problem wantTheBest, that's what we're here for. – DaOgre Apr 15 '11 at 17:09
3

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

Lekensteyn
  • 64,486
  • 22
  • 159
  • 192
Naftali
  • 144,921
  • 39
  • 244
  • 303
1

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.

Joeytje50
  • 18,636
  • 15
  • 63
  • 95