Why is echo faster than print?

不羁岁月 提交于 2019-12-23 20:01:24

问题


In PHP, why is echo faster than print?

They do the same thing... Why is one faster than the other?

Do they do exactly the same thing?


回答1:


echo and print are virtually (not technically) the same thing. The (pretty much only) difference between the two is that print will return the integer 1, whereas echo returns nothing. Keep in mind that neither is actually a function, but rather language constructs. echo allows you to pass multiple strings when using it as if it were a function (e.g., echo($var1, $var2, $var3)).

echo can also be shorthanded by using the syntax <?= $var1; ?> (in place of <?php echo $var1; ?>).

As far as which is faster, there are many online resources that attempt to answer that question. PHP Benchmark concludes that "[i]n reality the echo and print functions serve the exact purpose and therefore in the backend the exact same code applies. The one small thing to notice is that when using a comma to separate items whilst using the echo function, items run slightly faster."

It will really come down to your preference, since the differences in speed (whatever they actually are) are negligible.




回答2:


Print always returns 1, which is also probably why it's slower




回答3:


Print has a return value, this is the only difference.

The speed difference (if any) is so miniscule that it's not worth the effort thinking about micro optimisations like this, and it's absolutely not worth updating any old code to switch prints to echos. There are much better ways to speed up your site, if this is your goal.




回答4:


The differences are broken down here: PHP Performance: Echo & Print




回答5:


As my experience and knowledge, You are wrong. print is faster than echo in the loops autobahn and hypertexts.

Which is faster?

I'm implementing a test that shows the difference between print and echo.

$start = microtime(1);
for($i = 0; $i < 100000; $i++)
    echo "Hello world!";

echo "echo time: " . round(microtime(1) - $start, 5);

$start = microtime(1);
for($i = 0; $i < 100000; $i++)
    print "Hello world!";

echo "print time: " . round(microtime(1) - $start, 5);

result:

echo time: .09
print time: .04

Another reference is phpbench that shows this fact.

Comparision

Now its time to investigate that why print is faster than echo. When you are using loops, of course, php checks if echo has multiple values to print or not, but always print can take only one parameter and it's not needed to be checked in loops. also when there are multiple values for echo bad things come through, like converting them to string and streaming them, I do believe that in huge hypertexts these problem come through too because you are forcing php to process before printing. But in small jobs like printing, only a small string echo I good (if you consider concatenations) because it doesn't return anything like print.



来源:https://stackoverflow.com/questions/3570558/why-is-echo-faster-than-print

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!