开发者

PHP - Comments inside <?php echo <<<EOF

How do I comment out if I use this syntax?:

<?php
            echo <<<EOF

    Is it //Comment or <!-- Comment --> or something开发者_开发问答 else?    

            EOF;
            ?>

I'm a little bit confused. Thank you


You can't. The point of HEREDOC is that anything inside it will be part of the string. It exists to avoid having PHP meta characters (including comments) treated as such. Anything you put inside it will appear in the string (and thus, in this instance, be echoed to wherever the output is directed).

If the output is HTML, then you could include an HTML comment in it. That will still appear in the output, but anything parsing the HTML will treat it as a comment. Likewise, if the content is JS then you can use a JS comment, and so on.


You can't use a comment inside the heredoc syntax.

http://en.wikipedia.org/wiki/Here_document

It's a way to specify a literal string, literally.


Everything between the heredoc delimiters is interpreted literally, and that's the point of the heredoc syntax. Any HTML comments will be outputted as well, and PHP doesn't care that the browser will omit them.


It depends on the type of output you are echoing to. If you're echoing that data to an HTML page, you can use the <!-- --> syntax and the browser will see that as a comment. If you're outputting to a plaintext file, everything within the heredoc will be output (in truth, everything will be output when writing HTML as well, just the browser will interpret the HTML comment).

When I'm using heredoc syntax and need to comment the information inside, I typically use a (PHP-style) comment before the heredoc and reference any specific lines within by their line number, relative to the heredoc:

/* Write out default INI file.
 * L002: Log level. Possible values: debug,info,warning,error.
 */
echo <<<EOF
[global]
logging = error
...
EOF

Hope that helps.


To my knowledge you can't put comments inside the HEREDOC block. According to PHP documentation at http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc "Heredoc text behaves just like a double-quoted string, without the double quotes.".. and you can't put comments inside a double-quoted string.


Actually, there is a circuitous way of commenting inside an echo.

You can use the following syntax

<?php
'.(/*comment here*/ NULL).'

This line will be read as inner php and can contain comments. NULL has to be included because everything inside the inner php requires a value. NULL is the most convenient choice, since you dont actually want a value, just a comment.


Actually, you cannot use PHP comments in the middle of the string (neither in double quoted nor in single quoted nor heredoc strings). They will be shown literally.

In some rare cases it may be useful to simulate comments and strip them later from the string. For instance it could be useful if you are creating some kind of template engine. But certainly in most cases you should not do this, as it's bad practice.

You can build up your string using "\r\n" (using double quotes for the line breaks) and put the comments on the same line after the string, e.g. echo "line1\r\nline2"; //this outputs two lines

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜