PHP 中的多行字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9744192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi-line strings in PHP
提问by TheBlackBenzKid
回答by Madara's Ghost
Well,
好,
$xml = "l
vv";
Works.
作品。
You can also use the following:
您还可以使用以下内容:
$xml = "l\nvv";
or
或者
$xml = <<<XML
l
vv
XML;
Edit based on comment:
根据评论编辑:
You can concatenate strings using the .=
operator.
您可以使用.=
运算符连接字符串。
$str = "Hello";
$str .= " World";
echo $str; //Will echo out "Hello World";
回答by Dan Fabulich
PHP has Heredoc and Nowdoc strings, which are the best way to handle multiline strings in PHP.
PHP 有 Heredoc 和 Nowdoc 字符串,它们是 PHP 中处理多行字符串的最佳方式。
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
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;
A Nowdoc is like a Heredoc, but it doesn't replace variables.
Nowdoc 类似于 Heredoc,但它不替换变量。
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
$var is NOT replaced in a nowdoc.
EOD;
Beware that the end token EOD
must not be indented at all, or PHP won't acknowledge it.Also, you don't have to use "EOD
"; it can be any string you want.
请注意,结束标记EOD
不能完全缩进,否则 PHP 不会确认。此外,您不必使用“ EOD
”;它可以是您想要的任何字符串。
回答by Ryan
回答by eselk
Not sure how it stacks up performance-wise, but for places where it doesn't really matter, I like this format because I can be sure it is using \r\n (CRLF) and not whatever format my PHP file happens to be saved in.
不确定它如何在性能方面叠加,但对于它并不重要的地方,我喜欢这种格式,因为我可以确定它使用的是 \r\n (CRLF) 而不是我的 PHP 文件碰巧是的任何格式保存在。
$text="line1\r\n" .
"line2\r\n" .
"line3\r\n";
It also lets me indent however I want.
它还可以让我随心所欲地缩进。
回答by Xavi Esteve
Another solution is to use output buffering, you can collect everything that is being outputted/echoed and store it in a variable.
另一种解决方案是使用输出缓冲,您可以收集正在输出/回显的所有内容并将其存储在变量中。
<?php
ob_start();
?>line1
line2
line3<?php
$xml = ob_get_clean();
Please note that output buffering might not be the best solution in terms of performance and code cleanliness for this exact case but worth leaving it here for reference.
请注意,对于这种情况,输出缓冲在性能和代码清洁度方面可能不是最佳解决方案,但值得留在这里以供参考。
回答by Norbert Orzechowicz
Maybe try ".=" indead of "="?
也许尝试“。=”而不是“=”?
$xml="l";
$xml.="vv";
will give you "lvv";
会给你“lvv”;
回答by nathanjosiah
$xml="l\rn";
$xml.="vv";
echo $xml;
But you should really look into http://us3.php.net/simplexml
但你真的应该看看http://us3.php.net/simplexml
回答by Shawn Solomon
To put the strings "l" and "vv" on separate lines in the code alone:
将字符串 "l" 和 "vv" 单独放在代码中的不同行:
$xml = "l";
$xml .= "vv"
echo $xml;
In this instance you're saying to append .=
the string to the end of the previous version of that string variable. Remember that = is only an assignment operator so in your original code you're assigning the variable a new string value.
在这种情况下,您是说将.=
字符串附加到该字符串变量的先前版本的末尾。请记住,= 只是一个赋值运算符,因此在您的原始代码中,您要为变量分配一个新的字符串值。
To put the strings "l" and "vv" on separate lines in the echo alone:
将字符串 "l" 和 "vv" 单独放在 echo 中的单独行上:
$xml = "l\nvv"
echo $xml;
You don't need multiple strings in this instance, as the new line character \n
will take care of that for you.
在这种情况下,您不需要多个字符串,因为换行符\n
会为您处理这些问题。
To put the strings "l" and "vv" on separate lines in code and when echoing:
在代码中和回显时将字符串“l”和“vv”放在不同的行上:
$xml = "l";
$xml .= "\nvv"
echo $xml;