在 PHP 中,“<<<”是什么意思?代表?
例如:
$sql = <<<MySQL_QUERY
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
例如:
$sql = <<<MySQL_QUERY
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
我发现
Heredoc
和Nowdoc
在PHP
中都非常强大和有用,令我惊讶的是,迄今为止没有人提供更多示例来说明您可以做什么做。首先,
Heredoc
和Nowdoc
之间的区别很简单,Heredoc
:就像“”双引号字符串一样,您可以放入变量Nowdoc
:就像 '' 单引号字符串没有解析变量对于以下示例,我将仅显示
Heredoc
,按顺序要制作Nowdoc
只需将标记括在单引号内 -> “令牌”。特点和优点
也很有用。
简单示例
输出
配方
这有效,因为 HEREDOC将每个 \n 解释为实际行
创建小组件
<前><代码>
{$task['state']}
{$task['描述']}
查看任务待办事项
赫雷多克;
回显$组件; // 输出
}
?>
或者只放入一个字符串,然后使用 1 echo
文档
I found both
Heredoc
andNowdoc
extremelly powerfull and usefull inPHP
and I am surprise that no one have so far give more example of what you can do.First the difference between
Heredoc
andNowdoc
is simple,Heredoc
: Is like the "" double quote string you can put VariablesNowdoc
: Is like the '' single quote string no variable are parsedFor the following example I will only show the
Heredoc
, in order to make aNowdoc
just wrap the token inside single quotes -> 'TOKEN'.Features and Advantages
with nl2br .
Simple Example
output
Recipes
<br>
for each lineThis works because HEREDOC interpretes each \n as an actual line
Create small components
Or just put in one string then output with 1 echo
Documentation
为了得到一个清晰的想法:
如果我们使用
<<<
:结论:如果我们使用第一种方法,我们必须将其转换为
json_encode()
需要一些处理。相反,我们可以使用<<<
运算符来节省一些时间并获得一些干净的代码。 :)To get a clear idea:
If we use
<<<
:Conclusion: If we go with the 1st method we have to convert it into
json_encode()
which somehow requires some processing. Instead, We can use the<<<
operator to save some time and get some clean code. :)这是heredoc 语法。您可以通过放置
<<<
加上您选择的标记来启动heredoc字符串,并通过仅将标记(而不是其他任何东西!)放在新行上来终止它。为方便起见,有一个例外:允许您在结束分隔符后添加一个分号。例子:
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.Example:
这称为heredoc,它可以让您编写跨越多行的长文本。您可以将 PHP 变量放在那里,它们将替换为值。 CHART 这个词可以是任何东西。只需要在引用文本的开始处和停止处相同即可。
您可以通过附加多个带引号的字符串来完成相同的操作,但是对于像此 HTML 文本这样的扩展文档,这在大多数情况下会更清晰。还有一种称为 nowdoc 的东西,它就像 中的单引号字符串PHP,但它们不允许您在其中使用变量。
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.
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 nowdoc which is like a single quote string in PHP, but these won't let you use variables inside them.
它是使用 HEREDOC 语法。
It is the start of a string that uses the HEREDOC syntax.
这是 PHP 的
heredoc< /代码>
。
例子:
It's PHP's
heredoc
.Example:
这是一个定界符,对于长字符串,您不必担心引号之类的问题。如果您注意到“CHART”一词,然后有一行显示“CHART;”,则表示字符串的结尾。
使用此格式时要记住的重要一点是,无论您使用什么字符串来定义字符串结尾(例如本例中的 CHART),该单词都必须单独出现在一行上,后跟分号,并且 NO字符可以出现在同一行的分号之后,甚至是空格,否则 PHP 认为它是字符串的一部分。
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.
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.
这是 heredoc 语法。
It's the heredoc syntax.