发布于 2012-03-17 04:39:09
井,
$xml = "l
vv";
很管用。
您还可以使用以下内容:
$xml = "l\nvv";
或
$xml = <<<XML
l
vv
XML;
基于注释进行编辑:
可以使用.=
运算符连接字符串。
$str = "Hello";
$str .= " World";
echo $str; //Will echo out "Hello World";
发布于 2016-04-10 12:14:40
PHP有Heredoc和Nowdoc字符串,这是在PHP中处理多行字符串的最佳方法。
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
$var is replaced automatically.
EOD;
Nowdoc类似于Heredoc,但它不会替换变量。
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
$var is NOT replaced in a nowdoc.
EOD;
您不必使用"EOD
“作为开始/结束标识符;它可以是您想要的任何字符串。
注意缩进。在PHP7.3之前,结束标识符EOD
不能缩进,否则不会识别它。在PHP7.3和更高版本中,EOD
可以通过空格或制表符进行缩进,在这种情况下,将从heredoc/nowdoc字符串中的所有行中剥离缩进。
发布于 2013-09-27 02:22:06
不确定它在性能方面的表现如何,但对于不重要的地方,我喜欢这种格式,因为我可以确定它使用的是\r\n (CRLF),而不是我的PHP文件恰好保存为的任何格式。
$text="line1\r\n" .
"line2\r\n" .
"line3\r\n";
它还可以让我随心所欲地缩进。
https://stackoverflow.com/questions/9744192
复制相似问题