Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 856
wantTheBest Avatar asked Apr 15 '11 16:04

wantTheBest


3 Answers

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

like image 80
DaOgre Avatar answered Nov 16 '22 05:11

DaOgre


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;
like image 11
SeppoTaalasmaa Avatar answered Nov 16 '22 04:11

SeppoTaalasmaa


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

like image 2
Naftali Avatar answered Nov 16 '22 03:11

Naftali