PHP - <?php echo <<<EOF 内的注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6550021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Comments inside <?php echo <<<EOF
提问by Uli
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
我有点困惑。谢谢
回答by Quentin
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).
你不能。HEREDOC 的重点是它里面的任何东西都将成为字符串的一部分。它的存在是为了避免将 PHP 元字符(包括注释)视为这样。您放入其中的任何内容都将出现在字符串中(因此,在这种情况下,将回显到输出所指向的任何位置)。
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.
如果输出是 HTML,那么您可以在其中包含 HTML 注释。这仍然会出现在输出中,但任何解析 HTML 的内容都会将其视为注释。同样,如果内容是 JS 则可以使用 JS 注释,依此类推。
回答by Fosco
You can't use a comment inside the heredoc
syntax.
您不能在heredoc
语法中使用注释。
http://en.wikipedia.org/wiki/Here_document
http://en.wikipedia.org/wiki/Here_document
It's a way to specify a literalstring, literally.
这是一种从字面上指定文字字符串的方法。
回答by Blagovest Buyukliev
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.
heredoc 分隔符之间的所有内容都按字面解释,这就是 heredoc 语法的重点。任何 HTML 注释也会被输出,PHP 不在乎浏览器是否会忽略它们。
回答by Justin ??????
It depends on the type of output you are echo
ing 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).
这取决于您要执行的输出类型echo
。如果您将该数据回显到 HTML 页面,则可以使用该<!-- -->
语法,浏览器会将其视为注释。如果您输出到纯文本文件,则heredoc 中的所有内容都将被输出(实际上,编写HTML 时也将输出所有内容,只是浏览器会解释HTML 注释)。
When I'm using heredoc syntax and need to comment the information inside, I typically use a (PHP-style) comment beforethe heredoc and reference any specific lines within by their line number, relative to the heredoc:
当我使用heredoc 语法并需要注释里面的信息时,我通常在heredoc之前使用(PHP 样式)注释,并通过行号引用其中的任何特定行,相对于heredoc:
/* Write out default INI file.
* L002: Log level. Possible values: debug,info,warning,error.
*/
echo <<<EOF
[global]
logging = error
...
EOF
Hope that helps.
希望有帮助。
回答by Jan Molak
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.
据我所知,您不能在 HEREDOC 块中添加注释。根据http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc上的 PHP 文档,“Heredoc 文本就像一个双引号字符串,没有双引号。”...并且您不能在双引号字符串中放置注释。
回答by Willem van der Spek
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.
该行将被视为内部 php 并且可以包含注释。必须包含 NULL,因为内部 php 中的所有内容都需要一个值。NULL 是最方便的选择,因为您实际上不需要一个值,只需要一个注释。
回答by Karolis
Actually, you cannot use PHP comments in the middle of the string (neither in double quoted nor in single quoted nor heredocstrings). They will be shown literally.
实际上,您不能在字符串中间使用 PHP 注释(双引号、单引号和heredoc字符串都不能)。它们将按字面显示。
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 doublequotes for the line breaks) and put the comments on the same line afterthe string, e.g.
echo "line1\r\nline2"; //this outputs two lines
您可以使用“\r\n”(使用双引号作为换行符)构建字符串,并将注释放在字符串后面的同一行,例如
echo "line1\r\nline2"; //this outputs two lines