PHP中的这种语法是什么? [英] What's this kind of syntax in PHP?

查看:82
本文介绍了PHP中的这种语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$test= <<<EOF

....

EOF;

我从未见过.它有什么用?

I have never see it before. What's it used for?

推荐答案

这称为

This is called HEREDOC syntax, which is a way to define strings, on multiple lines, with variable interpolation.


引用手册页:


Quoting the manual page:

Heredoc文本的行为就像 双引号字符串,不带 双引号.这意味着引号 在heredoc中不需要 转义,但列出了转义码 以上仍然可以使用.变量是 扩展,但必须同样注意 表达复合体时采取 在heredoc中的变量与 字符串.

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.

(还有更多要阅读的内容,我没有从手册页进行复制粘贴)


并且,作为一个非常简单快捷的示例:


And, as a very quick and simple example:

$a = 'World';
$string = <<<MARKER
<p>
  Hello, $a!
</p>
MARKER;
echo $string;

它将为您提供以下输出:

It will give you this output:

Hello, World!

这个HTML来源:

<p>
  Hello, World!
</p>

这篇关于PHP中的这种语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆