PHP:连接 1 行还是多行更好?或者有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/431569/
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
PHP: Is it better to concatenate on 1 line or multiple lines? Or is there a difference?
提问by Darryl Hein
Is there a difference or is one better than the other of the following:
是否有区别或其中一个比以下另一个更好:
$var = '';
...
$var .= 'blah blah';
$var .= $var2;
$var .= 'blah blah';
Or
或者
$var = '';
...
$var .= 'blah blah' . $var2 . 'blah blah';
Is there a speed difference or is there a reason why you'd pick one over the other?
是否存在速度差异,或者您是否有理由选择一个而不是另一个?
采纳答案by Ivan
There is a small difference if you echo the string.
如果你回显字符串,会有一个小的区别。
echo $var1,$var2,$var3 // comma (echo with multiple arguments)
is slightly faster than
略快于
echo $var1.$var2.$var3 // dot (concat and echo)
回答by jmucchiello
Both PEZ and Topbit are correct. I just want to point out something I think looks better than has been seen here:
PEZ 和 Topbit 都是正确的。我只想指出一些我认为比这里看到的更好的东西:
$var = "line 1 \n";
$var .= "line 2 \n";
versus
相对
$var = "line 1 \n"
. "line 2 \n";
I much prefer the second one to the first one as visually it is obvious your result is the one string $var. It also prevents stupid bugs like:
我更喜欢第二个而不是第一个,因为在视觉上很明显你的结果是一个字符串 $var。它还可以防止愚蠢的错误,例如:
$var = "line 1 \n";
$var .= "line 2 \n";
$vat .= "line 3 \n";
$var .= "line 4 \n";
回答by PEZ
It doesn't matter for a few, short, strings. Use whatever is clearer to read.
对于一些简短的字符串来说并不重要。使用更清晰的阅读方式。
But if you have lots of them (say thousands) not so short ones, you'll probably want to keep them in an array and join them together. (As it happens such code is often as easy to read as concatenating the strings using ".=", so you're not sacrificing clarity on the altar of performance.)
但是如果你有很多(比如数千个)而不是那么短的,你可能希望将它们保存在一个数组中并将它们连接在一起。(碰巧这样的代码通常与使用“.=”连接字符串一样容易阅读,因此您不会牺牲性能祭坛的清晰度。)
EDIT: I don't think my above thinking is valid. PHP's strings are mutable, right?
编辑:我不认为我的上述想法是有效的。PHP 的字符串是可变的,对吧?
回答by chroder
Won't make a difference. Both are concatenation, so whatever is easiest to read is what you should use.
不会有什么不同。两者都是串联,所以最容易阅读的就是你应该使用的。
If you're after best-performing, try using an array and then implode it when you've finished.
如果您追求最佳性能,请尝试使用数组,然后在完成后内爆它。
$var = array();
$var[] = "line 1\n";
$var[] = "line 2\n";
$var[] = "line 3";
$var = implode('', $var);
Any way you do it the speed difference will be negligible unless you are working with thousands of big strings.
除非您使用数千条大字符串,否则无论您采用哪种方式,速度差异都可以忽略不计。
回答by grossvogel
For anything you're concatenating manually in code, performance probably doesn't matter, so readability is king. For me, this often means using heredocsyntax. I don't like the way it breaks my indentation structure, but it's especially nice when you want line breaks and/or tabs inserted correctly into your strings.
对于您在代码中手动连接的任何内容,性能可能并不重要,因此可读性是王道。对我来说,这通常意味着使用heredoc语法。我不喜欢它破坏我的缩进结构的方式,但是当您希望将换行符和/或制表符正确插入到您的字符串中时,它特别好。
回答by Alister Bulman
The fastest way to get a large amount of data into a single string, is output buffering to capture echo'd output.
将大量数据放入单个字符串的最快方法是输出缓冲以捕获回显输出。
ob_start();
/* many echos. For example, in a loop */
$outString = ob_get_contents(); // get the complete string
ob_end_clean();
In general, it's unlikely to make a significant difference to the speed of your program, so make it look obvious what you are doing, since you have to come back to fix/update it later anyhow. Readability is king.
一般来说,它不太可能对你的程序速度产生显着影响,所以让你在做什么看起来很明显,因为你以后无论如何都必须回来修复/更新它。可读性是王道。
回答by Hans
There is no meaningful difference. It's just a matter of coding preference.
没有有意义的区别。这只是编码偏好的问题。
回答by Nigel Atkinson
I had a hunch that making an array and then imploding might be the fastest way. I was wrong! However doing a $str = $str + $bla is REALLY slow by order of magnitude! It only matters if your doing a ton of concatenations. Here's my test code:
我有一种预感,制作一个数组然后内爆可能是最快的方法。我错了!然而,执行 $str = $str + $bla 的速度确实慢了一个数量级!仅当您进行大量连接时才重要。这是我的测试代码:
<?php
function doit($n) {
$str2concat = 'Esta es una prueba. ?Cuál manera es más rápida?';
$str = '';
// option 1
$start = microtime(true);
for( $i=0; $i <= $n; $i++ ) {
$str .= $str2concat;
}
$end = microtime(true);
echo " Concat: $n Iterations, duration:" . ($end-$start) . "\n";
// option 2
$str = '';
$start = microtime(true);
for( $i=0; $i <= $n; $i++ ) {
$str = $str . $str2concat;
}
$end = microtime(true);
echo "Concat2: $n Iterations, duration:" . ($end-$start) . "\n";
// option 3
$str = [];
$start = microtime(true);
for( $i=0; $i <= $n; $i++ ) {
$str[] = $str2concat;
}
$str = implode( $str );
$end = microtime(true);
echo "Implode: $n Iterations, duration:" . ($end-$start) . "\n\n";
}
doit( 5000 );
doit( 10000 );
doit( 100000 );
Here are the results that I got:
以下是我得到的结果:
Concat: 5000 Iterations, duration:0.0031819343566895
Concat2: 5000 Iterations, duration:0.41280508041382
Implode: 5000 Iterations, duration:0.0094010829925537
Concat: 10000 Iterations, duration:0.0071289539337158
Concat2: 10000 Iterations, duration:1.776113986969
Implode: 10000 Iterations, duration:0.013410091400146
Concat: 100000 Iterations, duration:0.06755805015564
Concat2: 100000 Iterations, duration:264.86760401726
Implode: 100000 Iterations, duration:0.12707781791687
回答by Nigel Atkinson
I don't think you'll notice for such a short situation. I typically decide for readability reasons which I'm going to use. I only split on two situations, otherwise on one line:
我不认为你会注意到这么短的情况。我通常出于可读性的原因决定我将要使用的内容。我只在两种情况下分开,否则在一行上:
a) Creating a multi-line response so the code somewhat mimics what the user will see. I've found it makes it easier to catch typos and mistakes this way.
a) 创建多行响应,以便代码在某种程度上模仿用户将看到的内容。我发现这样可以更容易地发现错别字和错误。
$var = "line 1 \n";
$var .= "line 2 \n";
b) line length. If the line will overflow my window I'll typically break it up so I don't have to go to the end of the line to read the end. Most languages support a line continuation character so I don't really need to break it physically in the code, but just a habit i have since I do work in some languages that don't have it.
b) 线长。如果该行会溢出我的窗口,我通常会将其分解,这样我就不必走到行尾阅读结尾。大多数语言都支持换行符,所以我真的不需要在代码中物理地打破它,但这只是我的一个习惯,因为我在一些没有它的语言中工作。

