Heredoc语法对我来说更干净,它对于多行字符串和避免引用问题确实很有用。回到过去,我曾经用它们来构造SQL查询:
$sql = <<<SQLselect * 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";
上面的pProblem是我刚刚引入的语法错误(缺少的转义引号),与此处的文档语法相反:
$x = <<<EOFThe point of the "argument" was to illustrate the use of here documentsEOF;
这有点风格,但是我将以下内容用作定义字符串的单,双和此处文档的规则:
- *当字符串是常量时,例如使用 *单 引号
'no variables here'
- 双 引号时,我可以把串在一行,并要求变量代换或嵌入的单引号
"Today is ${user}'s birthday" - 此处 记录了需要格式化和变量插值的多行字符串。



