PHP HTML 生成 - 使用字符串连接
关于PHP输出html的不同方法的问题; 这些之间的性能差异是什么:
方法 1 - 变量串联
$html = '';
$html .= '<ul>';
for ($k = 1; $k < = 1000; $k++){
$html .= '<li> This is list item #'.$k.'</li>';
}
$html .= '</ul>';
echo $html;
方法 2 - 输出缓冲
ob_start();
echo '<ul>';
for ($k = 1; $k < = 1000; $k++){
echo '<li> This is list item #',$k,'</li>';
}
echo '</ul>';
我怀疑不断修改和扩大变量会导致性能下降; 那是对的吗?
干杯!
谢谢 GaryF,但我不想得到有关架构的答案 - 这个问题是关于性能的。 关于哪一个更快似乎有一些不同的意见/测试,这就是为什么还没有一个公认的答案。
A question about different methods of outputting html from PHP; what are the performance differences between these:
Method 1 - variable concatenation
$html = '';
$html .= '<ul>';
for ($k = 1; $k < = 1000; $k++){
$html .= '<li> This is list item #'.$k.'</li>';
}
$html .= '</ul>';
echo $html;
Method 2 - output buffering
ob_start();
echo '<ul>';
for ($k = 1; $k < = 1000; $k++){
echo '<li> This is list item #',$k,'</li>';
}
echo '</ul>';
I suspect you get some performance hit from continually modifying and enlarging a variable; is that correct?
Cheers!
Thanks GaryF, but I don't want an answer about architecture - this question is about performance. There seem to be some different opinions / testing about which one is faster, which is why there is not an accepted answer as yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我没有看到提到的是,通常 PHP 人员必须与需要对 HTML 进行分类或以其他方式添加样式的设计人员合作,因此解决方案必须牢记这一点。
Something I haven't seen mentioned is that, usually, the PHP guy has to work with design people that need to class the HTML or add styles some other way, so the solution must bear that in mind.
我通常使用方法#1,这样我就可以将该 HTML 放在包含静态 HTML 的模板文件中的任何位置。 我尝试在 PHP 中保留尽可能多的 HMTL。 它使 PHP 变得更加简洁和简短,因为模板通常是一个单独的文件。
关于速度/性能,我认为差异会非常小。 通过输出缓冲,它也一直在扩大一个变量,尽管该变量不可访问,但它必须存储在某个地方。
我经常想知道打开和关闭
是否比将其全部放入
中然后立即回显要慢。
我认为最终我们讨论的是极其复杂的脚本中的毫秒。
哦,是的,方法 #1 更加灵活,因为您可以在任何地方
echo $html;
。I typically use method #1 so I can put that HTML any where in a template file that contains static HTML. I try to keep as much HMTL out of my PHP. It makes for much cleaner and shorter PHP, as the template is usually a separate file.
Regarding speed/performance, I'm thinking the difference will be very minor. With output buffering, it's also enlarging a variable all the time, although that variable isn't accessible, but it has to be stored somewhere.
I have often wondered if open and closing
<?php
is slower than just putting it all inside<?php
and then echo'ing all at once.I think in the end we are talking milliseconds in extremely complex scripts.
Oh yeah, method #1 is a lot more flexible as you can
echo $html;
anywhere.它有点旧,但是 Sara Golemon 的这篇文章可能会有所帮助。 AFAIK 输出缓冲函数非常快速且高效,
echo
也是如此,所以这就是我会使用的。It's a bit old, but this post by Sara Golemon will probably help. AFAIK the output buffering functions are quite fast and efficient and so is
echo
, so that's what I would use.撇开字符串连接本身的想法不谈,您实际上是在问(我认为)应该如何构建网页,并且让我印象深刻的是,任何形式的显式连接都可能是错误的做法。
尝试使用 模型-视图-控制模式 来构建数据,并传递它到一个简单的模板库(如 Smarty),并让它关心如何构建您的视图。
更好的分离,更少的担忧。
The idea of string concatenation itself aside, you're really asking (I think) how you should be building up web pages, and it strikes me that any form of explicit concatentation is probably the wrong thing to do.
Try using the Model-View-Control pattern to build up your data, and passing it to a simple templating library (like Smarty), and let it worry about how to build your view.
Better separation, fewer concerns.
确实,不断修改变量并重新打印它会产生轻微的开销。 然而,不这样做意味着在某个时刻脚本可能会完成运行(由于中断或类似情况),并且顺序 echo 语句将部分打印页面而不是什么也不打印。
It's true that there is a slight overhead in constantly modifying a variable and reprinting it. However, not doing that means that at some point the script may finish running (due to an interruption or similar), and the sequential echo statements will have partially printed the page rather than nothing.
只是几个想法:
输出缓冲会让页面看起来很慢,因为在整个脚本运行之前用户什么也看不到(尽管您的 #1 设置方式相同)。
php 中的字符串是可变的,因此连接并不像其他语言那样糟糕。 话虽如此,输出缓冲可能会稍微快一点,因为默认情况下为输入分配的空间相当大(根据 this)
最后我想说这实际上更多是一个风格问题。 如果您喜欢输出缓冲所带来的好处,那么这可能是您的最佳选择。
Just a couple of thoughts:
Output buffering can make you pages look slow, since the user sees nothing until the entire script has run (although the way you have #1 setup the same would hold true).
Strings in php are muteable, so concatenation is not nearly as bad as in some other languages. That being said, Output buffering might be just a tiny bit faster, as the space allocated for the input is fairly large by default (40K according to this)
In the end I'd say it's really more a question of style. If you like what output buffering buys you, then it is probably the way to go.
我本来打算写一个关于 PHP 字符串如何可变的长回复(与 C 或 C# 中的不可变字符串相反),但我想我只会链接到 我遇到的一篇较旧的帖子。 我基本上处理了您所询问的关于使用 stringbuilder 的 Java 和 C# 解决方案。
旁注: stringbuilder 解决方案类似于(未经测试):
I was going to type out a long reply about how PHP strings are mutable (opposed to immutable strings like in C or C#), but I think I'll just link to an older post I came across. I basically deals with what you're asking, in respect to the Java and C# solution of using a stringbuilder.
sidenote: the stringbuilder solution would be similar to (untested):
您的问题的答案也可以在这里找到:php:output[] w/ join vs $输出 .=
字符串连接是构建字符串的最快方法。
我还没有测试“echo”与字符串构建,但只要您不使用缓冲输出,由于顺序写入自刷新缓冲区,echo 应该是最快的。 (唯一的减慢是在冲洗中,即使您提前进行字符串连接也无法真正避免)
Answers to your question can also be found here: php: output[] w/ join vs $output .=
String concatenation is the fastest way to build strings.
I haven't tested "echo" vs string building, but as long as you're not using buffered output echo should be the fastest due to sequential writes to a self-flushing buffer. ( only slowdown being in the flush, which you won't really avoid even if you do string-concatenation in advance )