Need to add code comments inside heredoc

Well, I can’t add comments inside the heredoc block in the 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, of course (btw the form code above is greatly truncated for the sake of my question here).

But the dang comment ( okay this comment was..blah blah blah ) is also displayed in the browser. It appears in the browser as described 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 

Rearrangements in the demarcation of the comments I tried:

 // <-- // --> 

and....

 <-- // --> // 

FAIL in both cases so that I can comment inside the heredoc .

So how can I comment on my code in my heredoc s?

+7
source share
4 answers

This is by design. Once you become your heredoc EVERYTHING, you type until you are done, it is considered part of one long line. Your best bet is to break your HEREDOC, post your comment, and then start a new echo line.

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

As already mentioned, you can make HTML comments, but they will still be visible to anyone who views your source code.

+6
source

You can pass a comment line as a parameter to a function variable.

 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; 
+8
source

Try the following:

 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; 

now this is an HTML comment

+2
source

The easiest way to do this is to use the same tactics as SeppoTaalasmaa, 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 can insert comments in the next heredoc this way. This will also work if you do not define a function in the global scope.

0
source

All Articles