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 heredoc
s?
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;
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
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.