1.增加了可读性和可维护性,Heredoc是引用字符串的一个很好的替代方案。你不必转义引号,IDE或文本编辑器将使用正确的语法突出显示。
2.重复引用的字符串,这些字符串最终包含转义引号,IDE不会突出显示该语言的语法,这会导致可读性差,维护难度更大。
这里的文档语法对我来说比较干净,对于多行字符串和避免引用问题非常有用。我用来构建SQL查询:
$sql = <<<SQL
select *
from $tablename
where id in [$order_ids_list]
and product_name = "widgets"
SQL;
对我来说,引入语法错误的概率比使用引号的要低:
$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";
详细解读: