注意:很抱歉,如果这是一个非常简单的问题,但是我对代码的格式有些执迷不已。
我有一个类,该类具有返回字符串的函数,该字符串将构成电子邮件的正文。我希望此文本经过格式化,以便它在电子邮件中看起来很正确,而且也不会使我的代码显得时髦。这就是我的意思:
class Something { public function getEmailText($vars) { $text = 'Hello ' . $vars->name . ", The second line starts two lines below. I also don't want any spaces before the new line, so it's butted up against the left side of the screen."; return $text; } }
但也可以写成:
public function getEmailText($vars) { $text = "Hello {$vars->name},\n\rThe second line starts two lines below.\n\rI also don't want any spaces before the new line, so it's butted up against the left side of the screen."; return $text; }
但是换行和退货有什么关系呢?有什么不同?是\n\n相当于\r\r或\n\r?在行与行之间创建行距时应该使用哪个?
\n\n
\r\r
\n\r
然后是输出缓冲和heredoc语法的选项。
您如何处理在对象中使用长的多行字符串?
您应该使用HEREDOC或NOWDOC。
$var = "some text"; $text = <<<EOT Place your text between the EOT. It's the delimiter that ends the text of your multiline string. $var EOT;
Heredoc和Nowdoc之间的区别在于,将执行执行Heredoc中嵌入的PHP代码,而将按原样打印Nowdoc中的PHP代码。
$var = "foo"; $text = <<<'EOT' My $var EOT;
在这种情况下,$ text将具有value My $var。
My $var
注意: 结束前EOT;不应有空格或制表符。否则你会得到一个错误
EOT;