PHP practical advantages for using echo over print

Possible Duplicate:
Reference: Comparing PHP's print and echo

I am wondering about the practical and real advantages and reasons for using echo instead of print to write output to the screen.

In many areas of my code I have this sort of thing:

PRINT("<b>".$course_id."</b>");

I understand that print returns a value whereas echo doesn't, however i'm interested to know if there is any real reason to replace the instances in my code (and there are a few unfortunately) of print with echo ?

Is it just bad practice to use print in these sort of scenarios or is there reason and benefit to avoiding it?

I am using PHP 5.3.13.


From phpbench, echo() is faster than print() . But I think nobody will notice if your code is a few microseconds slower.


The difference is by far neglectable. There's no reason to refactor out uses of the print() function.


In contrast to 'echo' print returns a value, so

touch ($file) || print "Could not create $file";

is permitted but

touch ($file) || echo "Could not create $file";

is not.

According to Fabien Potencier 'print' uses one more opcode because it actually returns something: http://fabien.potencier.org/article/8/print-vs-echo-which-one-is-faster

So 'echo' should be a little, little bit faster than 'print'. I think most developers prefer echo to print, because using echo is a bit more flexible if you just want to render text.

You could replace the 'print'-calls with the search and replace-function of an IDE and a regex, if you desire to do so, but most of the performance bottlenecks of common applications are found elsewhere, i think.

链接地址: http://www.djcxy.com/p/57582.html

上一篇: 我如何在PHP中对数组和数据进行排序?

下一篇: 使用回声打印的PHP实用优势