在 PHP 中使用 heredoc 有什么好处?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5673269/
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
What is the advantage of using heredoc in PHP?
提问by danidacar
回答by Wes
The heredoc syntax is much cleaner to me and it is really useful for multi-line strings and avoiding quoting issues. Back in the day I used to use them to construct SQL queries:
Heredoc 语法对我来说更清晰,它对于多行字符串和避免引用问题非常有用。回到过去,我曾经使用它们来构建 SQL 查询:
$sql = <<<SQL
select *
from $tablename
where id in [$order_ids_list]
and product_name = "widgets"
SQL;
To me this has a lower probability of introducing a syntax error than using quotes:
对我来说,这比使用引号引入语法错误的可能性更低:
$sql = "
select *
from $tablename
where id in [$order_ids_list]
and product_name = \"widgets\"
";
Another point is to avoid escaping double quotes in your string:
另一点是避免在字符串中转义双引号:
$x = "The point of the \"argument" was to illustrate the use of here documents";
The pProblem with the above is the syntax error (the missing escaped quote) I just introduced as opposed to here document syntax:
上面的问题是我刚刚介绍的语法错误(缺少转义引号),而不是这里的文档语法:
$x = <<<EOF
The point of the "argument" was to illustrate the use of here documents
EOF;
It is a bit of style, but I use the following as rules for single, double and here documents for defining strings:
这有点风格,但我使用以下规则作为单、双和此处文档定义字符串的规则:
- Singlequotes are used when the string is a constant like
'no variables here'
- Doublequotes when I can put the string on a single line and require variable interpolation or an embedded single quote
"Today is ${user}'s birthday"
- Heredocuments for multi-line strings that require formatting and variable interpolation.
- 当字符串是常量时使用单引号,例如
'no variables here'
- 双引号时,我可以把串在一行,并要求变量代换或嵌入的单引号
"Today is ${user}'s birthday"
- 这里记录了需要格式化和变量插值的多行字符串。
回答by Jake Wilson
Heredoc's are a great alternative to quoted strings because of increased readability and maintainability. You don't have to escape quotes and (good) IDEs or text editors will use the proper syntax highlighting.
由于增加了可读性和可维护性,Heredoc 是引用字符串的绝佳替代品。您不必转义引号,(好的)IDE 或文本编辑器将使用正确的语法突出显示。
A verycommon example: echoing out HTML from within PHP:
一个非常常见的例子:从 PHP 中回显 HTML:
$html = <<<HTML
<div class='something'>
<ul class='mylist'>
<li>$something</li>
<li>$whatever</li>
<li>$testing123</li>
</ul>
</div>
HTML;
// Sometime later
echo $html;
It is easy to read and easy to maintain.
它易于阅读且易于维护。
The alternative is echoing quoted strings, which end up containing escaped quotes and IDEs aren't going to highlight the syntax for that language, which leads to poor readability and more difficulty in maintenance.
另一种方法是回显带引号的字符串,它最终包含转义的引号,并且 IDE 不会突出显示该语言的语法,这会导致可读性差和维护难度更大。
Updated answer for Your Common Sense
你的常识的更新答案
Of course you wouldn't want to see an SQL query highlighted as HTML. To use other languages, simply change the language in the syntax:
当然,您不希望看到突出显示为 HTML 的 SQL 查询。要使用其他语言,只需更改语法中的语言:
$sql = <<<SQL
SELECT * FROM table
SQL;
回答by cweiske
Some IDEs highlight the code in heredoc strings automatically - which makes using heredoc for XML or HTML visually appealing.
一些 IDE 会自动突出显示 heredoc 字符串中的代码 - 这使得将 heredoc 用于 XML 或 HTML 具有视觉吸引力。
I personally like it for longer parts of i.e. XML since I don't have to care about quoting quote characters and can simply paste the XML.
我个人喜欢它用于 ie XML 的较长部分,因为我不必关心引用引号字符并且可以简单地粘贴 XML。
回答by Your Common Sense
First of all, all the reasons are subjective. It's more like a matter of taste rather than a reason.
首先,所有的原因都是主观的。这更像是一个品味问题而不是一个原因。
Personally, I find heredoc quite useless and use it occasionally, most of the time when I need to get some HTML into a variable and don't want to bother with output buffering, to form an HTML email message for example.
就我个人而言,我发现 heredoc 非常无用,偶尔会使用它,大多数时候我需要将一些 HTML 放入变量中,并且不想打扰输出缓冲,例如形成 HTML 电子邮件。
Formatting doesn't fit general indentation rules, but I don't think it's a big deal.
格式不符合一般的缩进规则,但我认为这没什么大不了的。
//some code at it's proper level
$this->body = <<<HERE
heredoc text sticks to the left border
but it seems OK to me.
HERE;
$this->title = "Feedback";
//and so on
As for the examples in the accepted answer, it is merely cheating.
String examples, in fact, being more concise if one won't cheat on them
至于接受的答案中的例子,它只是作弊。
字符串示例,事实上,如果不作弊,它们会更简洁
$sql = "SELECT * FROM $tablename
WHERE id in [$order_ids_list]
AND product_name = 'widgets'";
$x = 'The point of the "argument" was to illustrate the use of here documents';
回答by asnyder
I don't know if I would say heredoc is laziness. One can say that doing anything is laziness, as there are always more cumbersome ways to do anything.
我不知道我是否会说 heredoc 是懒惰。可以说,做任何事情都是懒惰,因为做任何事情总是有更麻烦的方法。
For example, in certain situations you may want to output text, with embedded variables without having to fetch from a file and run a template replace. Heredoc allows you to forgo having to escape quotes, so the text you see is the text you output. Clearly there are some negatives, for example, you can't indent your heredoc, and that can get frustrating in certain situation, especially if your a stickler for unified syntax, which I am.
例如,在某些情况下,您可能希望输出带有嵌入变量的文本,而不必从文件中获取并运行模板替换。Heredoc 允许您不必转义引号,因此您看到的文本就是您输出的文本。显然有一些负面影响,例如,你不能缩进你的heredoc,这在某些情况下会令人沮丧,特别是如果你坚持统一语法,我就是。