php 超长字符串的链接输出性能问题~~~

WBOY
Release: 2016-06-06 20:11:36
Original
981 people have browsed it

在某循环体内,echo链接并输出一些变量、字符串时,怎么输出性能会更高?

1.用$html=""变量和“.”进行拼接,再输出

<code class="php">$html = "";
foreach() {
 $html .= ".....";
}
echo $html;
</code>
Copy after login
Copy after login

2.直接用一个echo+多个逗号输出

<code class="php">foreach() {
 echo "...",
 "....",
 "...";
 
 if () {
    echo "...."
 }
}
 </code>
Copy after login
Copy after login

3.多个echo输出

<code class="php">foreach() {
    echo "...";
    echo "...";
    echo "...";
    echo "...";
    echo "...";
}</code>
Copy after login
Copy after login

回复内容:

在某循环体内,echo链接并输出一些变量、字符串时,怎么输出性能会更高?

1.用$html=""变量和“.”进行拼接,再输出

<code class="php">$html = "";
foreach() {
 $html .= ".....";
}
echo $html;
</code>
Copy after login
Copy after login

2.直接用一个echo+多个逗号输出

<code class="php">foreach() {
 echo "...",
 "....",
 "...";
 
 if () {
    echo "...."
 }
}
 </code>
Copy after login
Copy after login

3.多个echo输出

<code class="php">foreach() {
    echo "...";
    echo "...";
    echo "...";
    echo "...";
    echo "...";
}</code>
Copy after login
Copy after login

要更快的输出字符串,你得了解字符串输出的过程。首先zend引擎拿到echo语句,开始检测要输出的内容一直到分号结束,然后再将内容中的所有变量转换成字符串,最后拼接在一起,输出。

想更快输出字符串从上面的输出过程着手,一方面改变变量转换成字符串速度,另一方面去掉字符串,不拼接,直接输出。

希望能帮到你。?

用逗号约等于多次echo,略快与拼装
参考文章

只要是输出性能都一样, 不如想办法把可以缓存的内容缓存在客户端,这样就不用纠结性能了

提个建议,将双引号换成单引号,因为php会解析双引号里面的变量,亲测。

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template