在 PHP 中,“<<<”代表什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3700042/
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
In PHP, what does "<<<" represent?
提问by Chrish
For example:
例如:
$sql = <<<MySQL_QUERY
回答by tdammers
That's heredoc syntax. You start a heredoc string by putting <<<
plus a token of your choice, and terminate it by putting only the token (and nothing else!) on a new line. As a convenience, there is one exception: you are allowed to add a single semicolon after the end delimiter.
这是heredoc 语法。您可以通过放置<<<
一个您选择的标记来开始一个 heredoc 字符串,并通过在一个新行上只放置一个标记(而不是其他任何东西!)来终止它。为方便起见,有一个例外:您可以在结束分隔符后添加一个分号。
Example:
例子:
echo <<<HEREDOC
This is a heredoc string.
Newlines and everything else is preserved.
HEREDOC;
回答by Pekka
It is the start of a string that uses the HEREDOC syntax.
它是使用HEREDOC 语法的字符串的开头。
A third way to delimit strings is the heredoc syntax: <<<.
After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
分隔字符串的第三种方法是heredoc 语法:<<<。
在这个运算符之后,提供了一个标识符,然后是一个换行符。紧随其后的是字符串本身,然后再次使用相同的标识符来关闭引号。
回答by hackartist
This is called a heredoc, and it lets you do a long piece of text that goes over several lines. You can put PHP variables in there and they will replace with the value. The word CHART can be anything. It just needs to be the same to start and stop where the quoted text begins.
这称为heredoc,它可以让您制作一段跨越多行的长文本。您可以将 PHP 变量放在那里,它们将替换为值。图表这个词可以是任何东西。它只需要在引用文本开始的地方开始和停止。
You could do the same thing by appending multiple quoted strings, but this is cleaner most of the time for extended documents like this HTML text. There is also something called a nowdocwhich is like a single quote string in PHP, but these won't let you use variables inside them.
您可以通过附加多个带引号的字符串来做同样的事情,但是对于像这样的 HTML 文本这样的扩展文档,这在大多数情况下更清晰。还有一种叫做nowdoc 的东西,它类似于 PHP 中的单引号字符串,但它们不允许您在其中使用变量。
回答by codaddict
回答by Russell Dias
It's the heredoc syntax.
这是heredoc 语法。
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
回答by ajacian81
It's a heredoc, for long strings that you don't have to worry about quotation marks and whatnot. If you notice the word CHART and then there's a line that says CHART;, that indicates the end of the string.
这是一个heredoc,对于长字符串,你不必担心引号之类的。如果您注意到CHART 这个词,然后有一行显示CHART;,表示字符串的结尾。
The important thing to remember when using this format is that whatever string you use to define the end of the string (such as CHART in this case), that word has to appear on a line on its own, followed by a semicolon, and NO characters can occur after the semicolon on the same line, even whitespace, otherwise PHP thinks it's part of the string.
使用这种格式时要记住的重要一点是,无论您使用什么字符串来定义字符串的结尾(例如本例中的 CHART),该单词都必须单独出现在一行中,后跟一个分号,并且 NO字符可以出现在同一行的分号之后,甚至是空格,否则 PHP 认为它是字符串的一部分。