考虑:

$xml = "l";
$xml = "vv";

echo $xml;

这将与vv相呼应。为什么以及如何为SimpleXML等做多行字符串?


当前回答

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”作为你的开始/结束标识符;它可以是你想要的任何字符串。

注意缩进。在PHP 7.3之前,结束标识符EOD完全不能缩进,否则PHP不会承认它。在PHP 7.3及更高版本中,EOD可以通过空格或制表符缩进,在这种情况下,缩进将从heredoc/nowdoc字符串中的所有行中删除。

其他回答

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”作为你的开始/结束标识符;它可以是你想要的任何字符串。

注意缩进。在PHP 7.3之前,结束标识符EOD完全不能缩进,否则PHP不会承认它。在PHP 7.3及更高版本中,EOD可以通过空格或制表符缩进,在这种情况下,缩进将从heredoc/nowdoc字符串中的所有行中删除。

好吧,

$xml = "l
vv";

的工作原理。

你也可以使用以下方法:

$xml = "l\nvv";

or

$xml = <<<XML
l
vv
XML;

根据评论编辑:

可以使用.=操作符连接字符串。

$str = "Hello";
$str .= " World";
echo $str; //Will echo out "Hello World";

PHP有两个内置方法HEREDOC和NOWDOC来处理多行字符串。HEREDOC也有语法<<<。

下面是使用HEREDOC实现多行PHP字符串的示例,

<?php
echo <<<EOT
My name is BB. I am printing some Text.
Now, I am printing SECOND LINE.
This should print a capital 'A': \x41
EOT;
?>

注意:如果您想通过使用本地服务器检查上述代码的正确输出,则必须在CMD或XAMPP Shell上运行完整的代码。

$xml="l\rn";
$xml.="vv";

echo $xml;

但你真的应该看看http://us3.php.net/simplexml

也许可以试试。”=" indead of "="?

$xml="l";
$xml.="vv";

会给你“lvv”;